Creating a function with display

2 Ansichten (letzte 30 Tage)
amateurintraining
amateurintraining am 29 Sep. 2017
Beantwortet: Steven Lord am 29 Sep. 2017
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?

Antworten (1)

Steven Lord
Steven Lord am 29 Sep. 2017
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.

Kategorien

Mehr zu Word games finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by