Class and Path Folders

Class and Path Folders

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.

Path Folders

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 with the 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, MyClass1.m, MyClass2.m, and so on).

See the path function for information about the MATLAB path.

Class Folders

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 (without the @ 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 constitute code).

Define only one class per folder. All files must have a .m extension (for example, @MyClass/MyClass.m, @MyClass/myMethod.m, 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 classdef file. See for more information.

Access to Functions Defined in Private Folders

Private folders contain functions that are accessible only from functions defined in folders immediately above the private folder (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 methods block with the Access attribute defined a protected).

No Class Definitions in Private Folders

You cannot put class definitions in private folders because doing so would not meet the requirements for class or path folders.

Class Precedence and MATLAB Path

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:

  • Class fldr1/foo.m takes precedence over the class fldr3/@foo because it is before fldr3/@foo on the path.

  • Class fldr3/@foo takes precedence over function 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).

  • Function fldr2/foo.m takes precedence over class fldr5/foo.m because it comes before class fldr5/foo.m on the path and because class fldr5/foo.m is not in a class folder. Classes not defined in class folders abide by path order with respect to functions.

  • Class fldr3/@foo takes precedence over class fldr4/@foo; therefore, the method bar is not recognized as part of the foo class (which is defined only by fldr3/@foo).

  • If fldr3/@foo/foo.m does not contain a classdef keyword (i.e., it is a MATLAB class prior to Version 7.6), then fldr4/@foo/bar.m becomes a method of the foo class defined in fldr3/@foo.

Previous Behavior of Classes Defined in Class Folders

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.

Was this topic helpful?