How can I use a for loop to name multiple variables?

2 次查看(过去 30 天)
I have data for a range of walking and running speeds, and the number of speeds changes for each subject. Currently I have something like this, where the "_1" after "Head" and the number after "Walk" refers to the speed:
Head_1 = Walk1(:,1:eaVar); Head_2 = Walk2(:,1:eaVar); Head_3 = Walk3(:,1:eaVar);
Is there a way to put the speed number in a loop to shorten the code? The number of speeds varies from 7 to 22. I need each speed to be in its own matrix.
Many thanks.

采纳的回答

Matt Tearle
Matt Tearle 2011-3-26
Obligatory answer: don't. In your case, a 3-D array is probably the best option.
But if you have no option (because the Walk variables already have a number in them), use num2str to make a string to represent the command, including the variable name, then eval to evaluate that command. Something like
str = ['Head(:,:,k) = Walk',num2str(k),'(:,1:eaVar);'];
eval(str);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by