How to add values to structure array without loop

5 ビュー (過去 30 日間)
Durga Lal Shrestha
Durga Lal Shrestha 2015 年 12 月 8 日
コメント済み: Somaye Hamedi Bazaz 2018 年 11 月 24 日
Consider the following loop:
values = [2 5 6 4 8 9 10 5 15 7]
for i=1:10
arr(i).myfield = values(i);
end
How can this be done without loop?
Thank you.

採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 8 日
T = struct('myfield', num2cell(values));
arr(1:length(T)) = T;
In the special case that arr does not already exist, it can be done with the one line
arr = struct('myfield', num2cell(values));
  4 件のコメント
Durga Lal Shrestha
Durga Lal Shrestha 2015 年 12 月 8 日
Thanks. It works now.
Somaye Hamedi Bazaz
Somaye Hamedi Bazaz 2018 年 11 月 24 日
Great!!!!!!!!!!!! thank you

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by