how to get struct array field as a vector?

I have a struct array, and a field like data.age and I want to get the ages as a vector v for which I can write for example : v(v>18), because data.age(data.age>18) gives me an error message.

1 件のコメント

Stephen23
Stephen23 2015 年 10 月 12 日
編集済み: Stephen23 2015 年 10 月 12 日
Accessing fields of non-scalar structures is covered quite well in the documentation:
It is also useful to understand comma separated lists:

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

 採用された回答

Thorsten
Thorsten 2015 年 10 月 12 日

15 投票

age = [data.age];
agegt18 = age(age>18);

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 10 月 12 日

2 投票

v = [data.age];
data(v>18)
Or more compactly,
data([data.age]>18)

2 件のコメント

Mr M.
Mr M. 2015 年 10 月 12 日
Thanks, and what if the filed contains strings, and I want to select elements for which string equals to a given string? Something like this: data({data.gender}=='male')
Thorsten
Thorsten 2015 年 10 月 12 日
編集済み: Thorsten 2015 年 10 月 12 日
Use
data(strcmp({data.gender}, 'male'))

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

Tin Aye Chit Chit
Tin Aye Chit Chit 2019 年 5 月 15 日

0 投票

Please check the code, that code can't give me the answer I want.
for i = 1:1:n
V=index{i};
for j = 1:1:size(V,2)
S{i,j}.e = nodeArch.node(V(1,j)).energy;
S{i,j}.t = nodeArch.node(V(1,j)).type;
S{i,j}.g = nodeArch.node(V(1,j)).G;
end
end
The S have more column than V. What is wrong in that code?

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2015 年 10 月 12 日

回答済み:

2019 年 5 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by