2x1 structure array to structure array
조회 수: 14 (최근 30일)
이전 댓글 표시
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?
댓글 수: 4
Cedric
2017년 10월 2일
Thank you James!
PS: well, it remains the duty of the OP to care for former questions/answers and it should not be up to us to check their history before answering.
James Tursa
2017년 10월 2일
Frankly, your suggestion to move that schedule input to the last argument and use nargin makes a lot more sense than what is going on in this thread. I wonder why OP didn't implement it ...
채택된 답변
James Tursa
2017년 10월 2일
편집: James Tursa
2017년 10월 2일
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];
댓글 수: 12
James Tursa
2017년 10월 2일
편집: James Tursa
2017년 10월 2일
@per: That works too. OP needs to decide how robust the function needs to be vs how much restriction to put on that 1st input. I prefer the former, but OP may prefer otherwise ...
per isakson
2017년 10월 2일
@James: Agree! Here is a way to prescribe the field names.
>> sas = struct( 'f1',{}, 'f2',{} )
sas =
0x0 struct array with fields:
f1
f2
>> s.f1=1;
>> s.f2=2;
>> sas = [ sas, s ]
sas =
f1: 1
f2: 2
and trying to add a structure with different fields
>> sas = struct( 'f1',{}, 'f2',{} );
>> s.f1=1;
>> s.f3=3;
>> sas = [ sas, s ]
Error using horzcat
Number of fields in structure arrays being concatenated do not match. Concatenation of
structure arrays requires that these arrays have the same set of fields.
>>
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!