Methods and Functions

The Methods Block

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

Method Calling Syntax

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.

    Note:   Always use case sensitive method names in your MATLAB code.

Methods In Separate Files

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, or, if the @-folder is contained in one or more package folders, then the top-level package folder must be 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

Define the Method Like Any Function

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.

Methods That Must Be In the classdef File

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 Setter and Getter Methods)

Specifying Method Attributes in classdef File

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.

Defining Static Methods in Separate Files

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

Using Separate Files for Methods

The example, Update Graphs Using Events and Listeners uses multiple files for class definition.

Private Methods

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.

More Detailed Information On Methods

See How to Use Methods for more information about methods.

Class-Related Functions

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 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 Packages Create Namespaces for more information on packages

Overloading Functions and Operators

Overload MATLAB functions for your class by defining a class method with the same name as the function you want to overload. MATLAB dispatches to the class method when the function is called with an instance of the class. See Overloading Functions for Your Class for more information.

You can also overload MATLAB arithmetic, logical, relational, and indexing operators by defining class methods with the appropriate names. See Implementing Operators for Your Class for a list of the functions to overload.

See the handle class for a list of operations defined for that class, which are inherited by all classes deriving from handle.

Was this topic helpful?