How to add more values to an existing structure?

119 ビュー (過去 30 日間)
Benson Gou
Benson Gou 2021 年 6 月 10 日
コメント済み: dpb 2021 年 6 月 10 日
Dear All,
I have an existing structure A which has several fields. Each field have 100 values. Now I want to add 20 more values to each existing field for the existing structure A. I used the following code but I got 2 structures which is not what I want.
A = [A; struct(fileds, values)]
Thanks for your help.
Benson
  3 件のコメント
Benson Gou
Benson Gou 2021 年 6 月 10 日
Hi, dpb,
I do not want to add a New Field to structure A. I want to add 20 more values to each existing field for the existing structure A.
Thanks.
Benson
dpb
dpb 2021 年 6 月 10 日
Oh, ok.
Use either setfield() or the dot notation referencing the desired field. NB: you can reference fields via variables via parentheses syntax such as
S.X=randi(10,4,1); % create dummy struct
vn='X'; % variable containing field name
S.(vn)=[S.(vn); randi(23,12,1)]; % add another dozen elements
Remember you can also get the struct fieldnames programmatically so don't have to write explicit strings as above with
>> vn=fieldnames(S)
vn =
1×1 cell array
{'X'}
>>

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

採用された回答

Max Heiken
Max Heiken 2021 年 6 月 10 日
A.existingField = [A.existingField; newValues];

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by