I wanna create vector of structures

58 views (last 30 days)
Jonghun Yun
Jonghun Yun on 31 Jul 2015
Edited: Azzi Abdelmalek on 31 Jul 2015
Why
"for i=1:10 vec(i)=struct('a',i, 'b',20-i, 'c',i+3); end"
does not work?
How can I correct it to create vectors of structures?
  1 Comment
Stephen23
Stephen23 on 31 Jul 2015
Edited: Stephen23 on 31 Jul 2015
for k = k:-1:1
X(k).a = k;
X(k).b = 20-k;
X(k).c = k+3;
end

Sign in to comment.

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 31 Jul 2015
Edited: Azzi Abdelmalek on 31 Jul 2015
ii=1:10
vec=struct('a',num2cell(ii),'b',num2cell(20-ii),'c',num2cell(ii+3))

Muthu Annamalai
Muthu Annamalai on 31 Jul 2015
You may also try, in addition to Azzi's code,
ii=1:10;
vec = arrayfun ( @(i) struct('a',i, 'b',20-i, 'c',i+3), ii );
depending on your stylistic preference.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!