MATLAB: Acces data in nested structure

MATLABnestest structure

Hi everyone,
I have a structure like this:
S.various_substructures.value1.x and S.various_substructures.value1.y
various_substructures are structures of varying fieldnames while the following substructures have the same names. The last structure contains arrays x and y.
I tried it with arrayfun without succes. Any idea to acces the data without a loop?
TIA, Rob

Best Answer

"Any idea to acces the data without a loop?"
Not really.
You can either use an explicit for loop or an implicit loop using arrayfun or structfun:
>> S.A.value1.x = 1;
>> S.A.value1.y = 11;
>> S.B.value1.x = 2;
>> S.B.value1.y = 22;
>> S.C.value1.x = 3;
>> S.C.value1.y = 33;
>> X = structfun(@(s)s.value1.x,S)
X =
1
2
3
>> Y = structfun(@(s)s.value1.y,S)
Y =
11
22
33