How do I loop through a structure field which contains multiple arrays?

조회 수: 1 (최근 30일)
Hi all,
I have a big structure which contains different substructures.
The structure is called 'subjects' and contains information for each subject.
However, I am interested in angles, which are located in subjects.sessions.trials(1).data.angles.
subjects.sessions.trials(1).data.angles contains 28 fields, all being a 1289x1 double, which all have names of different angles.
I want to plot the 28 angles by looping through, but I cannot manage to select what I need in the for loop:
for i = 1:NSessions
figure();
plot(subjects.sessions.trials(i).data.angles. ......);
end
Attached is a snapshot of what the structure field looks like.matlab.PNG
I tried creating a cell array with the 28 names and then use
for i = 1:NSessions
for k = 1:NAngleName
figure();
plot(subjects.sessions.trials(i).data.angles.AngleName{k});
end
end
But this is not working.
Thanks in advance.
  댓글 수: 2
KSSV
KSSV 2019년 1월 4일
Read about getfields and getfieldnames.
Inti Vanmechelen
Inti Vanmechelen 2019년 1월 4일
Thank you for your response KSSV.
I did read about getfields, but using 'getfields' still gives me 1x1 struct containing 28 fields which are all 1289x1 doubles. And it is those 1289x1 doubles which I would like to select, which is currently not workin out.

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

채택된 답변

Daniele Mascali
Daniele Mascali 2019년 1월 4일
If you want to make your last attempt work, you might use eval:
eval(['plot(subjects.sessions.trials(i).data.angles.',AngleName{k},');']);
  댓글 수: 3
Daniele Mascali
Daniele Mascali 2019년 1월 4일
You are right.
However, I was not aware of Dynamic Field References. One very simple solution is
plot(subjects.sessions.trials(i).data.angles.(AngleName{k}));
Thanks for the link!!
Inti Vanmechelen
Inti Vanmechelen 2019년 1월 4일
I figured that out myself about 10 seconds before your reply :)
Thanks!

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

추가 답변 (0개)

카테고리

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