There are two types of folders that can contain class definitions.
Path folders — Folder name does not use an @ character and is itself on the MATLAB® path. Use this type of folder when you want multiple classes in one folder. However, the entire class definition must be in one file.
Class folders — Folder name begins with an @ character followed by the class name. The folder is not on the MATLAB path, but its parent folder is on the path. Use this type of folder when you want to use multiple files for one class definition.
for information about the MATLAB path.
Put class definition files in folders that are on the MATLAB path. Class definitions placed in path folders behave like any ordinary function with respect to precedence—the first occurrence of a name on the MATLAB path takes precedence over all subsequent occurrences.
The name of the file must match the name of the class, as specified
classdef key word. Using a path folder
eliminates the need to create a separate class folder for each class.
However, the entire class definition, including all methods, must
be contained within a single file.
.../path_folder/MyClass1.m .../path_folder/MyClass2.m .../path_folder/MyClass2.m
A class folder must be contained in a path folder, but is not
itself on the MATLAB path. Place the class definition file inside
the class folder, which can also contain separate method files. The
class definition file must have the same name as the class folder
@ character) and the class definition
(beginning with the
classdef key word) must appear
in the file before any other code (white space and comments do not
.../parent_folder/@MyClass/MyClass.m .../parent_folder/@MyClass/myMethod1.m .../parent_folder/@MyClass/myMethod2.m
Define only one class per folder. All files must have a
You must use a class folder if you want to use more than one
file for your class definition. MATLAB treats any
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. See Methods In Separate Files for more information.
Private folders contain functions that are accessible only from
functions defined in folders immediately above the
If a class folder contains a
private folder, only
the class (or classes) defined in that folder can access functions
defined in the
private folder. Subclasses do not
have access to superclass private functions.
If you want a subclass to have access to the private functions
of the superclass, a better approach is to define the functions as
protected methods of the superclass (that is, in a
Access attribute defined a
You cannot put class definitions in private folders because doing so would not meet the requirements for class or path folders.
When multiple class definition files with the same name exist, the precedence of a given file is determined by its location on the MATLAB path. All class definition files before a class on the path (whether in a class folder or not) take precedence. The class takes precedence over all class definition files occurring later on the path.
For example, consider a path with the following folders, containing the files indicated:
|Order in Path||Folder and File||File Defines|
Here is the logic that MATLAB applies to determine which
foo to call:
fldr1/foo.m takes precedence over the
fldr1/foo.m is before
fldr3/@foo takes precedence over function
fldr3/@foo is a class in a class
fldr2/foo.m is not a class
Classes in class folders take precedence over functions
fldr2/foo.m takes precedence over
fldr2/foo.m comes before class
fldr5/foo.m is not in a class folder
Classes that are not defined in class folders obey the path order with respect to functions.
fldr3/@foo takes precedence over class
bar is not recognized
as part of the
foo class that is defined only by
fldr3/@foo/foo.m does not contain a
(that is, it is a MATLAB class prior to Version 7.6), then
a method of the
foo class defined in
In MATLAB Versions 5 through 7, class folders do not shadow other class folders having the same name, but residing in later path folders. Instead, the class the combination of methods from all class folders having the same name define the class. This is no longer true.
For backward compatibility, classes defined in class folders always take precedence over functions and scripts having the same name, even those that come before them on the path.