I wanna create vector of structures

조회 수: 10 (최근 30일)
Jonghun Yun
Jonghun Yun 2015년 7월 31일
편집: Azzi Abdelmalek 2015년 7월 31일
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
Stephen23
Stephen23 2015년 7월 31일
편집: Stephen23 2015년 7월 31일
for k = k:-1:1
X(k).a = k;
X(k).b = 20-k;
X(k).c = k+3;
end

댓글을 달려면 로그인하십시오.

답변 (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