How do I pass an object to a function

66 views (last 30 days)
David Devoy
David Devoy on 20 Apr 2017
Commented: David Devoy on 20 Apr 2017
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?

Answers (2)

Jan
Jan on 20 Apr 2017
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 Comment
Stephen23
Stephen23 on 20 Apr 2017
David Devoy's "Answer" moved here:
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

Sign in to comment.


Steven Lord
Steven Lord on 20 Apr 2017
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.
  1 Comment
David Devoy
David Devoy on 20 Apr 2017
Many thanks for your answer. It all seems to make sense but I'll need to read it through a couple of times to be sure I've got it.
I have previously done some OO programming in Java and this is a bit of a mixed blessing when it comes to working with Matlab.
In terms of OO principals it's exactly the same so it is very helpful.
The problems start with the implementation which is totally different. So I know I should be able to do something I just can't work out how sometimes.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!