Zip across variables in workspace in a for loop

3 Ansichten (letzte 30 Tage)
Anna Kasdan
Anna Kasdan am 2 Apr. 2018
Bearbeitet: Matt J am 3 Apr. 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

Akzeptierte Antwort

Matt J
Matt J am 2 Apr. 2018
Bearbeitet: Matt J am 3 Apr. 2018
C=struct('violin',violin,'cello',cello,'piano',piano};
C=structfun( @(i) double( reshape(i,[S(2)*S(3), S(1)]) ) , C ,'uni', 0)

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by