How Can I create struct field in a loop?

65 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

prasanth s
prasanth s il 18 Gen 2020
'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

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by