Creating a function with display

2 次查看(过去 30 天)
Create a function with the following header:
function [ schedule ] = addGameStruct ( schedule , hometeam , awayteam ,
homescore , awayscore )
where: schedule is a structure (with fields hometeam, awayteam, homescore, awayscore, winner) that holds the current data and will be expanded to include a new game, hometeam is a character array of the home team, awayteam is a character array of the away team, homescore is the home team’s final score and awayscore is the away team’s final score.
Your function should be able to reproduce the following sequence:
>> calSchedule = addGameStruct (struct , UNC , Cal , 30 , 35)
calSchedule =
struct with fields :
hometeam : UNC
awayteam : Cal
homescore : 30
awayscore : 35
winner : Cal
I have attempted to do this:
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
schedule=zeros(6,1);
schedule(1)=disp('struct with fields:');
schedule(2)=disp(['hometeam:',hometeam]);
schedule(3)=disp(['awayteam:',awayteam]);
schedule(4)=disp(['homescore:',homescore]);
schedule(5)=disp(['awayscore:',awayscore]);
schedule(6)=disp(['winner:',winner]);
But I keep on receiving an error, saying:
Error using disp
Too many output arguments.
Error in addGameStruct (line 10)
schedule(1)=disp('struct with fields:');
How do I write a function to display the text and the inputted value?

回答(1 个)

Steven Lord
Steven Lord 2017-9-29
The disp function is defined not to return any output arguments, which is why you receive that error. But even if it did, that would not satisfy the requirements of your problem statement. See this documentation page for an introduction to how to create a struct array. You should be able to adapt the first block of code on that page to create the struct array your problem requires. Instead of creating fields named name, billing, and test as the documentation example does you will create fields hometeam, awayteam, etc.

类别

Help CenterFile Exchange 中查找有关 Word games 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by