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 commentaires

James Tursa
James Tursa le 2 Oct 2017
@Cedric: I didn't see that post. It was a good answer you made and I voted for it.
Cedric
Cedric le 2 Oct 2017
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
James Tursa le 2 Oct 2017
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 ...

Connectez-vous pour commenter.

 Réponse acceptée

James Tursa
James Tursa le 2 Oct 2017
Modifié(e) : James Tursa le 2 Oct 2017
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 commentaires

If I do that, I get an error:
Error using vertcat
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.
Jan
Jan le 2 Oct 2017
@amateurintraining: The check the sizes of the arrays you want to concatenate. What do you observe? What might be a possible solution?
James Tursa
James Tursa le 2 Oct 2017
Modifié(e) : James Tursa le 2 Oct 2017
@amateurintraining:
You could change the concatenation to be conditional to account for that first call when the input schedule is empty:
if( isempty(schedule) || isempty(fieldnames(schedule)) )
schedule = new_schedule;
else
schedule = [schedule;new_schedule];
end
amateurintraining
amateurintraining le 2 Oct 2017
I think it's because the first schedule is not defined, and thus technically has no fields. This is why the arrays cannot be concatenated. But I was under the impression that using struct as the input for schedule would produce an empty array. Is this not true? Because when I type in [], my desired answer is produced.
James Tursa
James Tursa le 2 Oct 2017
Modifié(e) : James Tursa le 2 Oct 2017
A simple test:
>> struct
ans =
1x1 struct array with no fields.
>> isempty(struct)
ans =
0
No, it is not empty! But my latest post accounts for using either [] or struct as the first input.
Side note: This behavior matches what other functions do when passed no input arguments. E.g., zeros produces a 1x1 scalar 0, ones produces a 1x1 scalar 1, nan produces a 1x1 scalar nan, etc. So the behavior for struct when passed no input arguments seems appropriate in this sense.
amateurintraining
amateurintraining le 2 Oct 2017
Is there any possible way to do this without loops or if-else statements?
James Tursa
James Tursa le 2 Oct 2017
Do what without loops? I don't see any loops.
I'm sorry, I was mistaken about the loops. I meant is there any way to embed the code to account for the empty first schedule instead of using the if-else statement:
if( isempty(schedule) || isempty(fieldnames(schedule)) )
schedule = new_schedule;
else
schedule = [schedule;new_schedule];
end
James Tursa
James Tursa le 2 Oct 2017
Modifié(e) : James Tursa le 2 Oct 2017
Why are you opposed to using the if-else statement? It is simple and robust, does the job, and is easy to understand. Just add a comment explaining why it is there, and some comments at the front stating the acceptable inputs and outputs of the function. If you really don't like it, then go back to the single statement schedule = [schedule;new_schedule] and require that the user input [] as the 1st input argument on the 1st call.
Do you have pre-existing variables containing lots of teams and scores that you would like to put into a single struct? Is that what your real problem is?
@amateurintraining, Is this what you look for?
>> sas = struct.empty
sas =
0x0 struct array with no fields.
>> s.f1 = 1;
>> sas = [ sas, s ]
sas =
f1: 1
James Tursa
James Tursa le 2 Oct 2017
Modifié(e) : James Tursa le 2 Oct 2017
@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 ...
@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.
>>

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by