How to get an array of all field elements of a 1xN structure with many fields

9 ビュー (過去 30 日間)
Leo Simon
Leo Simon 2021 年 10 月 2 日
コメント済み: Leo Simon 2021 年 10 月 3 日
In this thread. @Sebastian asked how to get an array of all field elements of a 1xN structure. @AdamDanz answered, but his answer only applies to a single specified field of a struct. I want be able to loop through all fields of a struct and exact all elements of each field.
For example
S(1).a = 1; S(2).a = 2 ; S(1).b = 3 ; S(2).b = 4; S(1).c = [ 1, 2]; S(2).c = [ 3, 4]
From @AdamDanz's answer, I can write
[S(:).a]
[S(:).b]
etc., but naturally one wants to be able to loop thru all fields of S, as in
Fields = fieldnames(S);
for ii= 1:numel(Fields);
field = Fields{ii};
eval(['[S(:).' field ']']);
end
There has to be a less kludgy way of doing this!!! Thanks!
  1 件のコメント
Stephen23
Stephen23 2021 年 10 月 2 日
編集済み: Stephen23 2021 年 10 月 2 日
"There has to be a less kludgy way of doing this!!! "
For a start, you can trivially remove that very ugly and inefficient EVAL by using dynamic fieldnames:
i.e. replace this slow, inefficient, complex, anti-pattern code:
eval(['[S(:).' field ']']) % ugh, do NOT do this!
with this neat, simple, and very efficient code:
[S(:).(field)]
or equivalently just this:
[S.(field)]
See also:
Using a FOR loop is probably the most efficient solution to your original question.

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

採用された回答

Matt J
Matt J 2021 年 10 月 2 日
編集済み: Matt J 2021 年 10 月 2 日
I would recommend the attached file
S(1).a = 1; S(2).a = 2 ; S(1).b = 3 ; S(2).b = 4;
T=scalarize_struct(S)
T = struct with fields:
a: [1 2] b: [3 4]
  3 件のコメント
Matt J
Matt J 2021 年 10 月 2 日
scalarize_struct works for your modified example, too
S(1).a = 1; S(2).a = 2 ; S(1).b = 3 ; S(2).b = 4; S(1).c = [ 1, 2]; S(2).c = [ 3, 4];
T=scalarize_struct(S)
T = struct with fields:
a: [1 2] b: [3 4] c: {[1 2] [3 4]}
Leo Simon
Leo Simon 2021 年 10 月 3 日
scalarize_struct woks perfectly, thanks very much!

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

その他の回答 (1 件)

Jan
Jan 2021 年 10 月 2 日
Fields = fieldnames(S);
for ii= 1:numel(Fields);
field = Fields{ii};
[S(:).(field)]
end
[...] is the horizontal concatenation, so the line [S(:).(field)] is equivalent to:
cat(2, S(:).(field))
Maybe you want to join the vectors vertically, then use cat(1, ...). If the arrays have different sizes, you need a cell array:
{S(:).(field)}
  1 件のコメント
Leo Simon
Leo Simon 2021 年 10 月 3 日
Thanks @Jan, I accepted @Matt's answer because was a bit easier to work with in my case.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by