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 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. 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
MATLAB treats any.m file in the class folder as a method of the class. 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.
To define a method in a separate file in the class folder, create the function in a file with the
.m extension. Do not use the
end keywords in that file. Name the file with the function name, as with any function.
myFunc.m file, implement the method:
function output = myFunc(obj,arg1,arg2) ...% code here end
It is a good practice to declare the function signature in the
classdef file in a methods block:
classdef MyClass methods output = myFunc(obj,arg1,arg2) end ... end
If you 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
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
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
function output = staticFunc1(arg1,arg2) ... end
function staticFunc2 ... end
Define the following methods in the
classdef file. You cannot define these methods in separate files:
All functions that use dots in their names, 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