How to add variable fields in struct?

16 ビュー (過去 30 日間)
Eduard Mazur
Eduard Mazur 2021 年 4 月 30 日
コメント済み: Stephen23 2021 年 4 月 30 日
Hello all,
So i have a struct like this:
structExampe.(field(n))=struct('a', a, 'b', b);
and I want to add field in the same struct:
structExampe.(field(n))=struct('c', c);
and have all 3 variable inside struct (a,b,c)
How I should do this??
Thanks for the help!

採用された回答

Matt J
Matt J 2021 年 4 月 30 日
structExampe.(field(n)).c=c;
  2 件のコメント
Eduard Mazur
Eduard Mazur 2021 年 4 月 30 日
Thank you for the response, of course you are right.
But I need another solution, let me a bit change my example:
structExampe.(field(n))=struct('a', a, 'b', b, 'c', c);
structExampe.(field(n))=struct('d', d, 'e', e, 'c', f);
So, is it possible to do it in one line, or I need always make new statement like:
structExampe.(field(n)).d=d;
structExampe.(field(n)).e=e;
structExampe.(field(n)).f=f;
If I have lot more variables?
Stephen23
Stephen23 2021 年 4 月 30 日
"If I have lot more variables?"
Use a loop.
There is nothing stopping you from using two (or more) dynamic fieldnames.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by