Calling a functions fieldname without saying its name.

조회 수: 6 (최근 30일)
Cameron Tiegs
Cameron Tiegs 2020년 6월 1일
댓글: Cameron Tiegs 2020년 6월 1일
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'

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 6월 1일
편집: Mohammad Sami 2020년 6월 1일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by