Access automatically substructures from loop

1 回表示 (過去 30 日間)
V.D-C
V.D-C 2020 年 3 月 24 日
コメント済み: V.D-C 2020 年 3 月 26 日
Good morning everybody, I hope everything is good for you,
I am working with large structures and I want to automatize some tasks. If we take the structure that I attached to this message, how do I select the nth matrix of the structure ? Without writing "Example.hello" ?
I would like to do the same as when we take the value of a matrix in the nth position ( matrix(n)).
Have a nice day
  1 件のコメント
Stephen23
Stephen23 2020 年 3 月 24 日
編集済み: Stephen23 2020 年 3 月 24 日
Note that field order is NOT fixed and can change. Code that assumes that fields are in a particular order will be fragile.
As Steven Lord wrote, most likely you would be much better off using dynamic fieldnames:

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

採用された回答

Steven Lord
Steven Lord 2020 年 3 月 24 日
Rather than depending on the order in which the fields of the struct array are displayed (which can change -- in the following example s and snew contain the same data just in a different order)
s = struct('b',2,'c',3,'a',1)
snew = orderfields(s)
I would use dynamic field names, potentially in conjunction with the fieldnames function, or maybe (if you want to iterate through all the fields of the struct) structfun.
fn = 'b';
b1 = s.(fn)
b2 = snew.(fn)
sSquared = structfun(@(x) x.^2, s)
  1 件のコメント
V.D-C
V.D-C 2020 年 3 月 26 日
Thank you for your answer, I will continue in this new direction !

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by