Effacer les filtres
Effacer les filtres

Zip across variables in workspace in a for loop

3 vues (au cours des 30 derniers jours)
Anna Kasdan
Anna Kasdan le 2 Avr 2018
Modifié(e) : Matt J le 3 Avr 2018
Hi all,
I am having a bit of trouble (I am hoping this is actually a really simple fix to something I am doing wrong) creating a for loop that accesses variables I have in my workspace. I have three variables in my workspace (violin, cello, piano) all of the same size 14*128*657. The following code to reshape the variables works, but the problem is when I try and turn it into a for loop.
S = size(violin); % check to make sure all datasets are the same size
violin = reshape(violin,[S(2)*S(3), S(1)]);
violin = double(violin);
piano = reshape(piano,[S(2)*S(3), S(1)]);
piano = double(piano);
cello = reshape(cello,[S(2)*S(3), S(1)]);
cello = double(cello);
for i=['violin', 'cello', piano']
i = reshape(i,[S(2)*S(3), S(1)]);
i = double(i);
end
I have tried several things (including using curly brackets, etc.) but get the error: Error using reshape To RESHAPE the number of elements must not change.
Thanks for all the help in advance!
Anna

Réponse acceptée

Matt J
Matt J le 2 Avr 2018
Modifié(e) : Matt J le 3 Avr 2018
C=struct('violin',violin,'cello',cello,'piano',piano};
C=structfun( @(i) double( reshape(i,[S(2)*S(3), S(1)]) ) , C ,'uni', 0)
  1 commentaire
Anna Kasdan
Anna Kasdan le 2 Avr 2018
awesome, this is so helpful - thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by