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 object. You can assign values to properties in the class constructor. Terminate all method functions with an end statement.
classdef ClassName methods function obj = ClassName(arg1,arg2,...) obj.Prop1 = arg1; ... end function normal_method(obj,arg1,...) ... end end methods (Static = true) function static_method(arg1,...) ... end end end
MATLAB differs from languages like C++ and Java™ in that there is no special hidden class instance passed to all methods. You must pass an object of the class explicitly to the method. The left most argument does not need to be the class instance, and the argument list can have multiple objects.
See Determining Which Method Is Invoked for more information.
See alsoStatic Methods for information on methods that do not require instances of their class.
[warning]Always use case sensitive method names in your MATLAB code.[/warning]
You can define class methods in files that are separate from the class definition file, with certain exceptions (see Methods That Must Be In the classdef File). To use multiple files for a class definition, put the class files in a folder having a name beginning with the @ character followed by the name of the class. Ensure that the parent folder of the @-folder is on the MATLAB path.
For example, the folder @MyClass must contain the file MyClass.m (which contains the classdef block) and can contain other methods and function defined in files having a .m extension. For example, the folder @MyClass might contain a number of files:
@MyClass/MyClass.m @MyClass/subsref.m @MyClass/subsasgn.m @MyClass/horzcat.m @MyClass/vertcat.m @MyClass/myFunc.m
To define a method in a separate file in the class @-folder, create the function in a separate file, but do not use a method block in that file. Name the file with the function name, as with any function.
You must put the following methods in the classdef file, not in separate files:
All functions that use dots in their names, including:
If you specify method attributes for a method that you define in a separate file, include the method signature in a methods block in the classdef block. For example, the following code shows a method with Access set to private in the methods block. The method implementation resides in a separate file. Do not include the function or end keywords in the methods block, just the function signature showing input and output arguments.
classdef ClassName % In a methods block, set the method attributes % and add the function signature methods (Access = private) output = myFunc(obj,arg1,arg2) end end
In a file named myFunc.m, in the @ClassName folder, define the function:
function output = myFunc(obj,arg1,arg2) ... end
Include the method signature in the file with the classdef block only if you want to specify attributes for that method. Otherwise, you can implement the method as a function in a separate file in the @-folder.
To create a static method, set the function’s Static attribute to true. List any static methods that you define in separate files in the @-class folder. List these methods in the static methods block in the classdef file. Include the input and output arguments with the function name. For example:
classdef ClassName ... methods (Static) output = staticFunc1(arg1,arg2) staticFunc2 end
You would then define the functions in separate files using the same function signature. For example:
function output = staticFunc1(arg1,arg2) ... end
The example, Example — Using Events to Update Graphs uses multiple files for class definition.
Use the Access method attribute to create a private method. You do not need to use a private folder.
[important]See Method Attributes for a list of method attributes.[/important]
See Class Methods for more information about methods.
You can define functions that are not class methods in the file that contains the class definition (classdef). Define subfunctions outside of the classdef – end block, but in the same file as the class definition. Subfunctions defined in classdef files work like subfunctions. You can call these subfunctions from anywhere in the same file, but they are not visible outside of the file in which you define them.
Subfunctions 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 block:
classdef MyClass properties PropName end methods function obj = MyClass(arg1) obj.PropName = arg1; end end % methods end % classdef function myUtilityFcn ... end
You also can create package functions, which require you to use the package name when calling these functions. See Scoping Classes with Packages for more information on packages