How do I pass an object to a function
61 次查看(过去 30 天)
显示 更早的评论
I have a Matlab variable of object type, i.e. an instance of a class but I can't properly pass the variable to a function because the function interprets the object variable as a simple variable. It can't see its object properties or use the class methods on it within my function.
Is this just a limitation of OO Matlab or is there some way of declaring in the function definition that the passed variable has object type?
0 个评论
回答(2 个)
Jan
2017-4-20
I do not understand the problem exactly.
In Matlab the type of inputs cannot be defined in the function definition. Functions do not interprete the input, but they have exactly what you have provided. So either there is a typo in your code and you do not provide what you are thinking you do as input. Or you are faced with the object oriented programming style: properties are available from the object's methods only, not from everywhere. This is called "encapsulating" and "data hiding" and is a programming fundamental.
Please post some code which reproduces the problem.
1 个评论
Stephen23
2017-4-20
I am trying to build a Markov chain Monte Carlo model and am trying to use a football (soccer) league as an example.
I have a class "Team" and a class "League" and the script:
% Team (points,league Position)
Dee=Team(30,1);
Hamilton=Team(32,2);
ITC=Team(25,3);
Killie=Team(35,4);
Staggies=Team(33,5);
Well=Team(32,6);
spfl=League(6);
spfl=spfl.addTeam(Dee);
which returns the error:
*Error using League/addTeam
Too many input arguments.
Error in Markov (line 12)
spfl=spfl.addTeam(Dee); *
I have attached my class definition files below
%classdef (Attributes) ClassName % default attributes are OK
classdef Team % define the team class
% properties (attrubuites)
properties
points;
leaguePos;
end % end properties
methods
function obj = Team(pts,lp) % Team constructor
obj.points = pts;
obj.leaguePos=lp;
end % end Team constructor
% method to return the name of the object
end % methods
end % end classdef
%classdef (Attributes) ClassName % default attributes are OK
classdef League % define the team class
% properties (attrubuites)
properties
tot_no_teams
cur_no_teams
data
end % end properties
methods
function obj = League(N) % league constructor
obj.tot_no_teams = N;
obj.cur_no_teams=0;
obj.data = cell(N,2);
% create a cell structure
end % end function
function obj = addTeam(Team) % add team
if League.tot_no_teams >= League.cur_no_teams+1
obj.no_teams=League.no_teams+1;
obj.dat(1,League.no_teams)=inputname(1);
obj.dat(2,League.no_teams)=Team;
else
disp('League Full');
end
end % end function
end % end methods
end % end classdef
Steven Lord
2017-4-20
Looking at the code you posted in the comment, I see the problem. As per the documentation, " Note: Nonstatic methods must include an explicit object variable as a function argument. The MATLAB language does not support an implicit reference in the method function definition. "
The definition of the addTeam method in the League class is:
function obj = addTeam(Team) % add team
When you invoke it as:
spfl=spfl.addTeam(Dee);
this is basically equivalent to:
spfl = addTeam(spfl, Dee);
Passing two inputs into a function defined to accept only one is an error. You need to define addTeam as:
function obj = addTeam(League, Team) % add team
To avoid confusion between the names of the classes and the names of the variables, I'd probably modify the names of the inputs slightly.
function obj = addTeam(theLeague, newTeam) % add team
It doesn't make a difference now, but if you were to add a Static method to either the Team class or the League class in the future it might.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!