Collapsing a structure by combining field names

6 ビュー (過去 30 日間)
Josh Tome
Josh Tome 2022 年 12 月 21 日
編集済み: Matt J 2022 年 12 月 25 日
Hello,
I have a structure which contains 4 fields (Forceplate1, Forceplate2, Forceplate3, Forceplate4). Each of those fields contain 3 sub-fields (Force, Moment, COP). Each of those sub-fields contain their own sub-fields (X, Y, Z).
I would like to combined these structures/fields together into one big structure which will have the field names...
Forceplate1_Force_X
Forceplate1_Force_Y
Forceplate1_Force_Z
Forceplate1_Moment_X
Forceplate1_Moment_Y
Forceplate1_Moment_Z
Forceplate1_COP_X
Forceplate1_COP_Y
Forceplate1_COP_Z
Forceplate2_Force_X
Forceplate2_Force_Y
Forceplate2_Force_Z... and so on.
Any help would be greatly appreciated!
  2 件のコメント
Stephen23
Stephen23 2022 年 12 月 22 日
編集済み: Stephen23 2022 年 12 月 22 日
"Any help would be greatly appreciated!"
Rather than forcing meta-data into fieldnames, better data design would probably store the meta-data in the fields themselves, or into the arrangement of the data (i.e. the array size). For example using a non-scalar structure, something like this (where rows correspond to Forceplate 1, 2, 3... and the columns correspond to X, Y, and Z):
S(1,1).Force = arrX % Forceplate 1
S(1,2).Force = arrY
S(1,3).Force = arrZ
S(1,1).Moment = arrX
S(1,2).Moment = arrY
S(1,3).Moment = arrZ
etc..
S(2,1).Force = arrX % Forceplate 2
S(2,2).Force = arrY
S(2,3).Force = arrZ
S(2,1).Moment = arrX
S(2,2).Moment = arrY
S(2,3).Moment = arrZ
etc..
This means that you can e.g.:
  • loop over the structure rows using basic indexing. One row is one complete data set for one Forceplate, for example the complete second forceplate data would be simply S(2,:)
  • quickly convert each column to a meaningful table using STRUCT2TABLE().
  • use a convenient comma-separated list syntax to help access/process the data:
In contrast, doing those kind of things will be much more complex with meta-data stuck in the fieldnames.
Matt J
Matt J 2022 年 12 月 22 日
Probably even better to combine the X,Y,Z data into a single numeric array, so that you can do vector arithmetic if needed:
S(1).Force = cat(3,arrX,arrY,arrZ) % Forceplate 1
S(1).Moment = cat(3,arrX,arrY,arrZ)
etc..
S(2).Force = cat(3,arrX,arrY,arrZ) % Forceplate 2
S(2).Moment = cat(3,arrX,arrY,arrZ)
etc..

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

採用された回答

Matt J
Matt J 2022 年 12 月 21 日
編集済み: Matt J 2022 年 12 月 25 日
[A,B,C]=ndgrid("Forceplate"+(1:4),...
["Force","Moment","COP"], ...
["X","Y","Z"]);
clear newstruct
for i=1:numel(A)
[a,b,c]=deal(A(i),B(i),C(i));
field=strjoin([a,b,c],"_");
newstruct.(field)=oldstruct.(a).(b).(c);
end
  1 件のコメント
Matt J
Matt J 2022 年 12 月 22 日
編集済み: Matt J 2022 年 12 月 25 日
An alternative, based on comment above:
Fields = ["Force","Moment","COP"];
clear newstruct
for i=1:4
for j=1:numel(Fields)
field=Fields(j);
arrX=oldstruct.("Forceplate"+i).(field).X;
arrY=oldstruct.("Forceplate"+i).(field).Y;
arrY=oldstruct.("Forceplate"+i).(field).Z;
newstruct(i).(field) = cat(3, arrX,arrY,arrZ);
end
end

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by