reference a structure array

Hello
My question is simple.
can I reference a structure array; i.e if I have this code
s = struct('f1', [1 2; 3 4], 'f2', [5 6; 7 8]);
if I have for loop to find do some operation on the matrices stored in f1 & f2 like the mean for example. how can I index f1 & f2 in structure s so each time I can call one in the loop??

 채택된 답변

Laura Proctor
Laura Proctor 2011년 10월 12일

2 개 추천

You can use the fieldnames function and then use parentheses around the fieldname when called with a variable, see the code below:
s = struct('f1', [1 2; 3 4], 'f2', [5 6; 7 8]);
fn = fieldnames(s)
for idx = 1:length(fn)
mv(idx) = mean(s.(fn{idx})(:));
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

질문:

2011년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by