MATLAB Object-Oriented Programming

(Joyce) #1
The left most argument does not need to be the class object, and the argument list can
have multiple objects. MATLAB dispatches to the method defined by the class of the
dominant argument. For more information, see “Method Invocation” on page 9-15.

Methods must be on the MATLAB path when called. For example, if you create an object
and then change your current folder to a folder from which the method file is not visible,
an error occurs when you call that method.

Always use case sensitive method names in your MATLAB code.

Ordinary Methods

Call ordinary methods using MATLAB function syntax or dot notation. For example,
suppose you have a class that defines ordinaryMethod. Pass an object of the defining
class and whatever arguments are required.

classdef MyClass
methods
function out = ordinaryMethod(obj,arg1)
...
end
end
end

Call ordinaryMethod using the object obj of the class and either syntax:

obj = MyClass;
r = ordinaryMethod(obj,arg1);
r = obj.ordinaryMethod(arg1);

Static Methods

Static methods do not require an object of the class. To call a static method, prefix the
method name with the class name so that MATLAB can determine what class defines the
method.

classdef MyClass
methods (Static)
function out = staticMethod(arg1)
...
end
end
end

5 Class Definition—Syntax Reference

Free download pdf