Convert a vector into a structure array field

x= [2; 8; 6; 3; 7; 8];
for i=1:numel(x)
s(i).x=x(i);
end
Do the same, but without cycle.

 採用された回答

Ray
Ray 2017 年 10 月 20 日

0 投票

If struct s does not exist yet, you could use:
s = struct('x', num2cell(x));

3 件のコメント

bbb_bbb
bbb_bbb 2017 年 10 月 20 日
編集済み: bbb_bbb 2017 年 10 月 20 日
Thank you. And if it does exist and I want to add a field x?
Walter Roberson
Walter Roberson 2017 年 10 月 20 日
if x is the same length as the number of entries in the struct array s, then
temp = num2cell(x);
[s.x] = temp{:};
bbb_bbb
bbb_bbb 2017 年 10 月 20 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2017 年 10 月 20 日

コメント済み:

2017 年 10 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by