for loop

Hi All,
I have a basic syntax question regarding a for loop. I am trying to change the matrix(struct) name in a loop to copy values in another array using the same loop variable as follows:
for k=1:16
tstats(k,1)=results(num2str(k)).tstat
end
I have struct matrices named from 'results1 to results16'. tstat is the field I am trying to extract from and write to tstat matrix.
I think the problem is with syntax.
Any help?
Regards,
AMD.

답변 (2개)

Walter Roberson
Walter Roberson 2012년 6월 11일

1 개 추천

댓글 수: 3

ARS
ARS 2012년 6월 11일
Hi Walter,
I will be grateful if you could solve the above coz its hard to get from the referred link.
Regards,
AMD
Walter Roberson
Walter Roberson 2012년 6월 11일
The basic message is: Don't Do That. Do not create lists of variables that you have to loop over. Instead create a single variable whose parts you loop over. Use an array or a cell array or a structure.
Jan
Jan 2012년 6월 11일
Your problem is not how to get the values from these variables, but it is caused by "I have struct matrices named from 'results1 to results16'" already. The link Walter has posted shows, that using "results{1}" to "results{16}" would be a smarter naming scheme.
It's like including the phone number to your name: It solves some problems, but in total the usage gets less efficient.

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

Kevin Holst
Kevin Holst 2012년 6월 11일

0 개 추천

You can't call or write to a variable like that in a loop, unfortunately. My suggestion would be to have a structure that contains all of your 'results##' structures. Then you could use something like this:
for k=1:16
tstats(k,1)=results.(['results' num2str(k)]).tstat
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

ARS
2012년 6월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by