Define methods as MATLAB® functions within a
methods block, inside the
classdef block. The constructor method has the same name as the class and returns an initialized object of the class. To create an object with property values that are unique to that instance, assign values to properties in the class constructor. Terminate all method functions with an
classdef ClassName properties PropertyName end methods function obj = ClassName(arg1,...) obj.PropertyName = arg1; ... end function ordinaryMethod(obj,arg1,...) ... end end methods (Static) function staticMethod(arg1,...) ... end end end
MATLAB differs from languages like C++ and Java® in that there is no special hidden class object passed to all methods. You must pass an object of the class explicitly to the method. The leftmost 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.
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.
Call ordinary methods using MATLAB function syntax or dot notation. For example, suppose that 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
ordinaryMethod using the object
obj of the class and either syntax:
obj = MyClass; r = ordinaryMethod(obj,arg1); r = obj.ordinaryMethod(arg1);
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
staticMethod using the syntax
r = MyClass.staticMethod(arg1);
See Static Methods for information on methods that do not require objects of their class.
Access method attribute to create a private method. You do not need to use a private folder.
See Method Attributes for a list of method attributes.
You can define functions that are not class methods in the file that contains the class definition (
classdef). Define local functions outside of the
classdef - end block, but in the same file as the class definition. Functions defined in
classdef files work like local functions. You can call these functions from anywhere in the same file, but they are not visible outside of the file in which you define them.
Local functions in
classdef files are useful for utility functions that you use only within that file. These functions can take or return arguments that are instances of the class but, it is not necessary, as in the case of ordinary methods. For example, the following code defines
myUtilityFcn outside the
classdef MyClass properties PropName end methods function obj = MyClass(arg1) obj.PropName = arg1; end end end % End of classdef function myUtilityFcn ... end
You also can create package functions, which require the use of the package name when calling these functions.
Overload MATLAB functions for your class by defining a class method with the same name as the function that you want to overload. MATLAB dispatches to the class method when the function is called with an instance of the class.
You can overload MATLAB arithmetic, logical, relational, and indexing operators by defining class methods with the appropriate names.
handle class for a list of operations defined for that class. All classes deriving from
handle inherit these methods.
The following rules apply to methods defined in separate files:
To specify attributes for a method defined in a separate file, declare this method in a methods block in the
classdef file. Specify the attribute values with the methods block.
Match the syntax declared in the methods block (if used) to the method's
The separate file must be in the class (
The class constructor method must be defined in the
classdef file. The constructor cannot be in a separate file.
delete methods must be defined in the
classdef file. The
delete method cannot be in a separate file.
All functions that use dots in their names must be defined in the
classdef file, including:
Converter methods that must use the package name as part of the class name because the class is contained in packages
Property set and get access methods
For more information on defining methods in separate files, see Methods in Separate Files