I wanna create vector of structures

5 次查看(过去 30 天)
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?

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-31
编辑:Azzi Abdelmalek 2015-7-31
ii=1:10
vec=struct('a',num2cell(ii),'b',num2cell(20-ii),'c',num2cell(ii+3))

Muthu Annamalai
Muthu Annamalai 2015-7-31
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.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by