Creating a function with display

2 ビュー (過去 30 日間)
amateurintraining
amateurintraining 2017 年 9 月 29 日
回答済み: Steven Lord 2017 年 9 月 29 日
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 Center および File ExchangeWord games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by