| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
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,arg2,...) ... end end methods (Static = true) function static_method(arg1,...) ... end end end
For classes that include many methods, you can use separate files for some or all the class methods. To use multiple files for a class definition, put the class file in an @-folder.
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:
Class constructor
Delete method
All functions that use dots in their names, including:
Converter methods that convert to classes contained in packages, which must use the package name as part of the class name.
Property set and get access methods (Property Access Methods)
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 classdef 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 end
In an M-file named myFunc.m, in the @-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.
See Method Attributes for a list of method attributes.
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
![]() | Specifying Properties | Events and Listeners | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |