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.
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.
You can locate class definition files in folders that are on the MATLAB path. These classes are visible on the path like any ordinary function. 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 (for example,
and so on).
for information about the MATLAB path.
A class folder must be contained by 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
Define only one class per folder. All files must have a
and so on).
You must use a class folder if you want to use more than one
file for your class definition. Methods defined in separate files
match the file name to the function name and must be declared in the
Private folders contain functions that are accessible only from
functions defined in folders immediately above the
(See Private Functions).
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, define the private 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 it on the path (whether in a class folder or not) take precedence and it 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:
fldr1/foo.m % defines class foo fldr2/foo.m % defines function foo fldr3/@foo/foo.m % defines class foo fldr4/@foo/bar.m % defines method bar fldr5/foo.m % defines class foo
The MATLAB language applies the logic in the following
list to determine which version of
foo to call:
fldr1/foo.m takes precedence
over the class
fldr3/@foo because it is before
fldr3/@foo takes precedence
fldr2/foo.m because it is a class
in a class folder and
fldr2/foo.m is not a class
(classes in class folders take precedence over functions).
fldr2/foo.m takes precedence
fldr5/foo.m because it comes before
fldr5/foo.m on the path and because class
not in a class folder. Classes not defined in class folders abide
by path order with respect to functions.
fldr3/@foo takes precedence
fldr4/@foo; therefore, the method
not recognized as part of the
foo class (which
is defined only by
fldr3/@foo/foo.m does not contain
classdef keyword (i.e., 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 is defined by the combination of methods from all class folders having the same name. This is no longer true.
Note that 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.