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.

 Accepted Answer

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

3 Comments

bbb_bbb
bbb_bbb on 20 Oct 2017
Edited: bbb_bbb on 20 Oct 2017
Thank you. And if it does exist and I want to add a field x?
if x is the same length as the number of entries in the struct array s, then
temp = num2cell(x);
[s.x] = temp{:};

Sign in to comment.

More Answers (0)

Categories

Asked:

on 20 Oct 2017

Commented:

on 20 Oct 2017

Community Treasure Hunt

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

Start Hunting!