Get line of vector from struct array field

4 ビュー (過去 30 日間)
Vojtech
Vojtech 2018 年 4 月 16 日
編集済み: Stephen23 2018 年 4 月 16 日
Let's have a structure:
my_struct(1).my_field = [11;12];
my_struct(2).my_field = [21;22];
I want to get vector of first values of the field 'my_field', that is:
temp = [my_struct.field];
result = temp(1,:);
Is there any more elegant way which does not use temporary variable and might be coded into one line? Something like:
result = [my_struct.field](1,:); % produces error

採用された回答

Stephen23
Stephen23 2018 年 4 月 16 日
編集済み: Stephen23 2018 年 4 月 16 日

"Is there any more elegant way which does not use temporary variable and might be coded into one line?"

No.

You could use arrayfun, but in my opinion this is much less elegant:

>> arrayfun(@(s)s.my_field(1),my_struct)
ans =
   11   21

その他の回答 (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