How to convert all of a structures fields into strings that represent the complete names.

71 ビュー (過去 30 日間)
I have a structure of variable numbers of fields. I want to display them on a GUI so that they can be changed by a user. So I want to display the field names just as you would see them in matlab.
For example, let's say the structure is
Offset = struct('X',0,'Y',0,'Z',0);
Rot = struct('Roll',0,'Pitch',0,'Yaw',0);
IMU = struct('Offset',Offset,'Rot',Rot);
EKFerrors = struct('MeasTimeout',1,'MaxBound',3);
EKF = struct('IMU',IMU,'Errors',EKFerrors,'InitMode',2);
So the list of elements of the structure are shown below
EKF.IMU.Offset.X
EKF.IMU.Offset.Y
EKF.IMU.Offset.Z
EKF.IMU.Rot.Roll
EKF.IMU.Rot.Pitch
EKF.IMU.Rot.Yaw
EKF.Errors.MeasTimeout
EKF.Errors.MaxBound
EKF.InitMode
I would like each of these in an array of strings or cells of strings that I can display on the GUI just as they are shown here (not their value, their name), but I would have an edit box so a user could enter in values. I just need the struct converted to these strings.

採用された回答

Stephen23
Stephen23 2022 年 4 月 28 日
編集済み: Stephen23 2022 年 4 月 28 日
The only general solution is to use a recursive function. Here is code which works for scalar structures, although you could extend it to include indexing into non-scalar structures.
Offset = struct('X',0,'Y',0,'Z',0);
Rot = struct('Roll',0,'Pitch',0,'Yaw',0);
IMU = struct('Offset',Offset,'Rot',Rot);
EKFerrors = struct('MeasTimeout',1,'MaxBound',3);
EKF = struct('IMU',IMU,'Errors',EKFerrors,'InitMode',2);
Z = myfun(EKF)
Z = 9×1 cell array
{'EKF.IMU.Offset.X' } {'EKF.IMU.Offset.Y' } {'EKF.IMU.Offset.Z' } {'EKF.IMU.Rot.Roll' } {'EKF.IMU.Rot.Pitch' } {'EKF.IMU.Rot.Yaw' } {'EKF.Errors.MeasTimeout'} {'EKF.Errors.MaxBound' } {'EKF.InitMode' }
function C = myfun(S)
assert(isstruct(S),'Input <S> must be a structure.')
assert(isscalar(S),'Input <S> must be scalar.')
C = {};
recfun(S,inputname(1))
%
function recfun(A,N)
F = fieldnames(A);
for k = 1:numel(F)
T = sprintf('%s.%s',N,F{k});
B = A.(F{k});
if isstruct(B)
assert(isscalar(B),'Structure <%s> must be scalar.',T)
recfun(B,T)
else
C{end+1,1} = T;
end
end
end
end
  1 件のコメント
John Petersen
John Petersen 2022 年 4 月 28 日
編集済み: John Petersen 2022 年 4 月 28 日
Perfect! And thanks for teaching me how to do recursive functions. I thought it might require it, but I wasn't sure how to keep the output building on itself.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 4 月 28 日
Use fieldnames():
Offset = struct('X',0,'Y',0,'Z',0);
Rot = struct('Roll',0,'Pitch',0,'Yaw',0);
IMU = struct('Offset',Offset,'Rot',Rot);
EKFerrors = struct('MeasTimeout',1,'MaxBound',3);
EKF = struct('IMU',IMU,'Errors',EKFerrors,'InitMode',2);
OffsetFields = fieldnames(Offset)
OffsetFields = 3×1 cell array
{'X'} {'Y'} {'Z'}
RotFields = fieldnames(Rot)
RotFields = 3×1 cell array
{'Roll' } {'Pitch'} {'Yaw' }
IMUFields = fieldnames(IMU)
IMUFields = 2×1 cell array
{'Offset'} {'Rot' }
EKFerrorsFields = fieldnames(EKFerrors)
EKFerrorsFields = 2×1 cell array
{'MeasTimeout'} {'MaxBound' }
EKFFields = fieldnames(EKF)
EKFFields = 3×1 cell array
{'IMU' } {'Errors' } {'InitMode'}

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by