MATLAB: 2×1 structure array to structure array

functionstructure array

I have a code:
function [ schedule ] = addGameStruct( schedule,hometeam,awayteam,homescore,awayscore )
%ADDGAMESTRUCT
% schedule is a structure with fields hometeam, awayteam, homescore,
% awayscore, and winner that holds the current data and will be expanded
% to include a new game
% hometeam: home team's final score
% awayscore: away team's final score
field1='hometeam';
field2='awayteam';
field3='homescore';
field4='awayscore';
field5='winner';
value1=hometeam;
value2=awayteam;
value3=homescore;
value4=awayscore;
value5='Cal';
schedule=struct(field1,value1,field2,value2,field3,value3,field4,value4,field5,value5);
schedule=[schedule;struct(field1,value1,field2,value2,field3,value3,field4,value4,field5,value5)];
end
That when running the command:
calSchedule=addGameStruct(struct,'UNC','Cal',30,35)
returns the answer:
calSchedule =
2×1 struct array with fields:
hometeam
awayteam
homescore
awayscore
winner
However, I want to produce a structure array like the following:
>> calSchedule = addGameStruct (struct , UNC , Cal , 30 , 35)
calSchedule =
struct with fields :
hometeam : UNC
awayteam : Cal
homescore : 30
awayscore : 35
winner : Cal
Why doesn't my current function produce a single structure array?

Best Answer

You explicitly add that 2nd struct element with this line:
schedule=[schedule;struct(field1,value1,field2,value2,field3,value3,field4,value4,field5,value5)];
Did you mean for your code to add another element to a passed in schedule? E.g.,
new_schedule = struct(field1,value1,field2,value2,field3,value3,field4,value4,field5,value5);
schedule = [schedule;new_schedule];
Related Question