MATLAB: Calling a functions fieldname without saying its name.

fieldnamesloopingstructures

Hi there,
I have a structure called 'AerodynamicData.Cx.ActuatorName' where x is a wildcard character and ActuatorName is a wildcard phrase.
For example, I have structure called 'AerodynamicData'. This structure has two fields called Cm and Cp. Inside Cm there are two fields. Inside Cp there is one field.
AerodynamicData.Cm.BodyFlap
AerodynamicData.Cm.Speedbrake
AerodynamicData.Cp.Aileron
Possible structures^^
Now my question is how do I loop through all the structures that have this naming convention >> 'AerodynamicData.Cx.ActuatorName'

Best Answer

You can use the function fieldnames and a for loop
%s = somestruct
f1 = fieldnames(s)
for i = 1:length(f1)
fn1 = f1{i};
s2 = s.(fn1);
f2 = fieldnames(s2)
for j = 1:length(f2)
fn2 = f2{j};
value = s2.(fn2);
% do something with the value
end
end