Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Jennifer
Jennifer am 26 Mär. 2011
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.

Akzeptierte Antwort

Matt Tearle
Matt Tearle am 26 Mär. 2011
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);

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices 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