How Can I create struct field in a loop?

조회 수: 85 (최근 30일)
Gianmarco Manzo
Gianmarco Manzo 2020년 1월 18일
댓글: Gianmarco Manzo 2020년 1월 19일
Hello to all,
I have a problem with the struct. I have two nested for loops. With the innermost one I create a struct called 'strutt'. Once I get out of the internal one I have to allocate the whole structure to another structure in the different fields. The problem is that the field is always overwritten. here is the code. Can you help me?
for j=1:4
for i=1:10
strutt.a(1,i)=importdata(...)
strutt.b(1,i)=importdata(...)
..
end
soluz=struct(sprintf('hi%d',j),strutt);
end
  댓글 수: 2
Stephen23
Stephen23 2020년 1월 18일
Rather than forcing numbers into fieldnames it would probably be more efficient to use a non-scalar structure

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

채택된 답변

prasanth s
prasanth s 2020년 1월 18일
'struct' command creates new structure. you have to create new structure at the starting of loop. add new fields to the structure inside the loop.
soluz=struct;
for j=1:4
for i=1:10
strutt.a(1,i)=1;
strutt.b(1,i)=2;
end
soluz.(sprintf('hi%d',j))=strutt;
end

추가 답변 (0개)

카테고리

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