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

17 次查看（过去 30 天）
Leo Simon 2021-10-2

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]
[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 个评论显示 -1更早的评论隐藏 -1更早的评论
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)]
Using a FOR loop is probably the most efficient solution to your original question.

### 采纳的回答

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 个评论显示 1更早的评论隐藏 1更早的评论
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 2021-10-3
scalarize_struct woks perfectly, thanks very much!

### 更多回答（1 个）

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 个评论显示 -1更早的评论隐藏 -1更早的评论
Leo Simon 2021-10-3
Thanks @Jan, I accepted @Matt's answer because was a bit easier to work with in my case.

### 类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

### Community Treasure Hunt

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

Start Hunting!