Method Invocation - MATLAB & Simulink (original) (raw)
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
, whereobj
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 ofdata
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 invokenewDataSet
to add data to the object. Use dot syntax to callnewDataSet
. 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.
Referencing Method Names with Expressions
You can invoke a class method dynamically by enclosing an expression in parentheses.
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 objecta
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 thedataSetSummary
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.
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 theplus
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, theplus
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 ofDocPolynom
objects. Construct aDocPolynom
instance.
p = DocPolynom([1 0 -2 -5])
This statement adds a double to the DocPolynom
instance. The DocPolynom
class is dominant over the built-indouble
class, even though the double
is the leftmost argument in 1 + p
. This code invokes theDocPolynom
plus
method to add the polynomials.
You can also specify the relative precedence of classes defined with theclassdef
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 theclassdef
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.