get a field value from all elements of a structure

87 ビュー (過去 30 日間)
Mike D.
Mike D. 2021 年 2 月 22 日
コメント済み: Adam Danz 2021 年 2 月 22 日
If I have a structure s with a field A(1) and A(2) and A(3), where each of these A fields has a field B, I can type:
s = struct
s.A(1).B = 351;
s.A(2).B = 879;
s.A(3).B = 229;
s.A(4).B = 654;
If I type s.A.B it will show all four values as four separate "ans", but if I type temp = s.A.B it only grabs the first value. How do I get all of these values of B into a double array? I could write a for-loop:
for i = 1 : size(s.A,2)
temp(i) = s.A(i).B;
end
Is there a one-liner?

採用された回答

Adam Danz
Adam Danz 2021 年 2 月 22 日
  2 件のコメント
Mike D.
Mike D. 2021 年 2 月 22 日
Thanks, I knew there must be an easy way.
Adam Danz
Adam Danz 2021 年 2 月 22 日
Good intuition. :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by