Method Invocation
MATLAB® classes support both dot and function syntaxes for invoking methods. This topic demonstrates both syntaxes and describes how MATLAB determines what method to invoke.
Dot and Function Syntaxes
To invoke a nonstatic method with one argument arg
, where
obj
is an object of the class that defines the method, use
dot syntax or function syntax.
obj.methodName(arg) methodName(obj,arg)
For example, dataSetSummary
stores a set of numeric data along with
the mean, median, and range of that data. The class defines two methods:
showDataSet
displays the current data stored in the data
property, and newDataSet
replaces the current value of
data
and calculates the mean, median, and range of that
data.
classdef dataSetSummary properties (SetAccess=private) data {mustBeNumeric} dataMean dataMedian dataRange end methods function showDataSet(obj) disp(obj.data) end function obj = newDataSet(obj,inputData) obj.data = inputData; obj.dataMean = mean(inputData); obj.dataMedian = median(inputData); obj.dataRange = range(inputData); end end end
Create an instance of dataSetSummary
and invoke
newDataSet
to add data to the object. Use dot syntax to call
newDataSet
. Because dataSetSummary
is a value
class, assign the result back to the original variable to preserve the
change.
a = dataSetSummary; a = a.newDataSet([1 2 3 4])
a = dataSetSummary with properties: data: [1 2 3 4] dataMean: 2.5000 dataMedian: 2.5000 dataRange: 3
Invoke the showDataSet
method, but use function syntax for this
call.
showDataSet(a)
1 2 3 4
Referencing Method Names with Expressions
You can invoke a class method dynamically by enclosing an expression in parentheses.
obj.(expression)
The expression must evaluate to a character vector or string that is the name
of a method. For example, these two statements are equivalent for an object
a
of class dataSetSummary
.
a.showDataSet a.("showDataSet")
This technique does not work when used with function syntax.
Indexing into the Result of Method Call
You can dot index into the result of any method that returns a value for which
dot indexing is defined, such as an object property or structure field name. For
example, add a new method returnSummary
to the
dataSetSummary
class that returns all of the stored data in a
struct.
function outStruct = returnSummary(obj) outStruct = struct("Data",obj.data,... "Mean",obj.dataMean,... "Median",obj.dataMedian,... "Range",obj.dataRange); end
Call returnSummary
and use dot indexing to return the median
of the data set.
a.returnSummary.Median
ans = 2.5000
For more information on indexing into the result of function calls, see Indexing into Function Call Results.
Determining Which Method Is Invoked
When dot syntax is used to invoke a method, MATLAB calls the method defined by the class of the object to the left of the
dot. For example, if classA
and classB
both define a
method called plus
, then this code always invokes the
plus
method defined by classA
.
A = classA; B = classB; A.plus(B)
No other arguments are considered. Methods of other arguments are never called, nor are functions.
In other syntaxes, MATLAB must determine which of possibly many versions of an operator or
function to call in a given situation. The default behavior is to call the method
associated with the leftmost argument. In both of these statements, the
plus
method defined by classA
is called.
objA + objB plus(objA,objB)
However, this default behavior can be changed when one object has precedence over another.
Object Precedence
Depending on how classes are defined, the objects of those classes can take precedence over other objects when it comes to method dispatching:
Classes defined with the
classdef
syntax have precedence over these MATLAB classes:double
,single
,int64
,uint64
,int32
,uint32
,int16
,uint16
,int8
,uint8
,char
,string
,logical
,cell
,struct
, andfunction_handle
.Classes defined with the
classdef
syntax can specify their relative precedence to other classes using theInferiorClasses
attribute.
In Representing Polynomials with Classes, the DocPolynom
class defines a plus
method that enables the addition of
DocPolynom
objects. Construct a
DocPolynom
instance.
p = DocPolynom([1 0 -2 -5])
p = x^3 - 2*x - 5
This statement adds a double to the DocPolynom
instance.
The DocPolynom
class is dominant over the built-in
double
class, even though the double
is the leftmost argument in 1 + p
. This code invokes the
DocPolynom
plus
method to add the polynomials.
1 + p
ans = x^3 - 2*x - 4
You can also specify the relative precedence of classes defined with the
classdef
syntax by listing inferior classes in a class
attribute. The InferiorClasses
attribute gives the class
higher precedence than the classes listed as arguments for the attribute. Define
the InferiorClasses
attribute in the
classdef
statement:
classdef (InferiorClasses = {?class1,?class2}) myClass
This attribute establishes a relative priority of the class being defined with the order of the classes listed. For more information, see Class Precedence.