필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Jennifer
Jennifer 2011년 3월 26일
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