Creating structure array that accumulates

2 次查看(过去 30 天)
How do you create a structure array such that the structure array has five fields that are initially empty then more entries are entered to make the structure array a 1x2 structure? For example, the array has fields: field1, field2, field3, field4, field5. And the first entry is empty. The next command adds another entry with value1,value2,value3,value4,and value5. Basically, the following entries will result in an accumulation of entries. I want to create a function that builds on itself.
Preferably, I would like to see how this is done without if-else statements.
Thank you in advance!
  1 个评论
Cedric
Cedric 2017-10-3
编辑:Cedric 2017-10-3
You could just pay attention to the answers that were already given to you, e.g. here where I spent time carefully explaining a couple approaches to you:
and then here for the same question later:
where you can see in the comments under your question that both James and I are wondering why you didn't pay attention to the first answer.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2017-10-3
S = struct ;
% marks in three subjects
S.Maths= [];
S.Physics = [] ;
S.Chemsitry = [] ;
%%five students marks
for i = 1:5
S(i).Maths= rand*100;
S(i).Physics = rand*100 ;
S(i).Chemsitry = rand*100 ;
end

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by