Create new field on struct from names on existing field

1 回表示 (過去 30 日間)
Mikel  Jimenez
Mikel Jimenez 2021 年 7 月 20 日
コメント済み: Mikel Jimenez 2021 年 7 月 20 日
Hello,
I have a struct in Matlab as follows:
Now, based on the "name" field, I would like to create a new field called "Color". This new field would have two values, "blue" or "red", depending on the names inside the "name" field. Specifically, if the names have he letters H1, H2 or H3, the color in the new field should be "blue", whether if the name has the letters H4, H5 or H6, it should be "red".
Anyone could help me with this?
Thanks in advance.
-Mikel

採用された回答

Matt J
Matt J 2021 年 7 月 20 日
編集済み: Matt J 2021 年 7 月 20 日
names={Stimuli.names};
idxBlue=contains(names,["H1" "H2" "H3"]);
idxRed =contains(names,["H4" "H5" "H6"]);
[Stimuli(idxBlue).Color]=deal("blue");
[Stimuli(idxRed).Color]=deal("red");
  1 件のコメント
Mikel  Jimenez
Mikel Jimenez 2021 年 7 月 20 日
Hi Matt,
It works perfectly! Thank you so much.
-Mikel

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

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