필터 지우기
필터 지우기

Calling sequential variable names in a for loop?

조회 수: 9 (최근 30일)
Kim
Kim 2013년 12월 14일
댓글: Kim 2013년 12월 14일
I have a set of variable names numbered sequentially. For example one set is
AllChDeltaPreStim1,2,3,4,5 (so there are 5 different variables with the prefix followed by n=1-5). I want to do a hilbert transform on all of them but I'm sure I don't have to write it out 5 times, right? I know how to write a for loop for each column/row in a variable but not this. I am definitely a beginner programmer so please help and forgive if stupid question! Thanks! Kim

채택된 답변

Image Analyst
Image Analyst 2013년 12월 14일
Yes you do need to call the code separately. However you could make it easier and make the code into a function so that you just need to call the function 5 times and all the heavy lifting is done in that function rather than in your main program.
results1 = YourHilbertFunction(AllChDeltaPreStim1);
results2 = YourHilbertFunction(AllChDeltaPreStim2);
results3 = YourHilbertFunction(AllChDeltaPreStim3);
results4 = YourHilbertFunction(AllChDeltaPreStim4);
results5 = YourHilbertFunction(AllChDeltaPreStim5);
It's not really many more lines, if any, than a for loop. If you don't want to do that, you can make AllChDeltaPreStim and arry.
  댓글 수: 1
Kim
Kim 2013년 12월 14일
Thank you! Never written a function but I'll try. Kim

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kevin Stanley-Adams
Kevin Stanley-Adams 2013년 12월 14일
편집: Kevin Stanley-Adams 2013년 12월 14일
Your best option is probably to store the contents of AllChDeltaPreStimX in an array rather than seperate variables. Then you can access the data in each iteration by using something like:
for i=0:5
%code
AllChDeltaPreStim(i)
%code
end
If your separate variables are matrices, I'd create a structure array and store them in that. I'm no Matlab expert though, so this might not be the optimum solution.
  댓글 수: 1
Kim
Kim 2013년 12월 14일
Thanks, I will definitely try that! Kim

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by