Main Content

Methods in Separate Files

Class Folders

You can define class methods in files that are separate from the class definition file, with certain exceptions (see Methods You Must Define in the classdef File).

To use multiple files for class definitions, put the class files in a folder having a name beginning with the @ character followed by the name of the class (this is called a class folder). Ensure that the parent folder of the class folder is on the MATLAB® path.

If the class folder is contained in one or more namespace folders, then the folder containing the top-level namespace folder must be on the MATLAB path.

For example, the folder @MyClass must contain the file MyClass.m (which contains the classdef block) and contains other methods and function defined in files having a .m extension. The folder @MyClass can contain a number of files:

@MyClass/MyClass.m
@MyClass/subsref.m
@MyClass/subsasgn.m
@MyClass/horzcat.m
@MyClass/vertcat.m
@MyClass/myFunc.m

Types of Method Files

MATLAB treats any function file in the class folder as a method of the class. Function files can be MATLAB code (.m), Live Code file format (.mlx), MEX functions (platform dependent extensions), and P-code files (.p). The base name of the file must be a valid MATLAB function name. Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores.

For information on defining methods as C++ MEX functions, see Using MEX Functions for MATLAB Class Methods.

Define Method in Function File

To define a method in a separate file in the class folder, create the function in a file. Do not use the method-end keywords in that file. Name the file with the function name, as with any function.

In the myFunc.m file, implement the method:

function output = myFunc(obj,arg1,arg2)
   % code here
end

For code readability, declaring the function signature in the classdef file in a methods block is good practice. You also must declare the function signature to set method attributes. If you do not declare the function signature in the classdef file, all attributes of the method take their default values. For example, a method without a signature in classdef has Access set to public, Sealed set to false, and so on.

Specify Method Attributes in classdef File

If you need to specify method attributes for a method that you define in a separate function file, include the method signature in a methods block in the classdef file. This methods block specifies the attributes that apply to the method.

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. Include only the function signature showing input and output arguments.

classdef MyClass
   methods (Access = private)
      output = myFunc(obj,arg1,arg2)
   end
end

In a file named myFunc.m, in the @MyClass folder, define the function:

function output = myFunc(obj,arg1,arg2)
   ...
end

Static Methods in Separate Files

To create a static method, set the method Static attribute to true and list the function signature in a static methods block in the classdef file. Include the input and output arguments with the function name. For example:

classdef MyClass
...
   methods (Static)
      output = staticFunc1(arg1,arg2)
      staticFunc2
   end
   ...
end

Define the functions in separate files using the same function signature. For example, in the file @MyClass/staticFunc1.m:

function output = staticFunc1(arg1,arg2)
   ...
end

and in @Myclass/staticFunc2.m:

function staticFunc2
   ...
end

Methods You Must Define in the classdef File

Define the following methods in the classdef file. You cannot define these methods in separate files:

  • Class constructor

  • All functions that use dots in their names, including:

    • Converter methods that must use the namespace name as part of the class name because the class is contained in namespaces

    • Property set and get access methods

Related Information

Related Topics