Calling a functions fieldname without saying its name.

5 views (last 30 days)
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'

Accepted Answer

Mohammad Sami
Mohammad Sami on 1 Jun 2020
Edited: Mohammad Sami on 1 Jun 2020
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

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!