Zip across variables in workspace in a for loop

3 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2018-4-2
编辑:Matt J 2018-4-3
C=struct('violin',violin,'cello',cello,'piano',piano};
C=structfun( @(i) double( reshape(i,[S(2)*S(3), S(1)]) ) , C ,'uni', 0)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by