Documentation

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. 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.

See the path function for information about the MATLAB path.

Path Folders

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 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.

.../path_folder/MyClass1.m
.../path_folder/MyClass2.m
.../path_folder/MyClass2.m

Class Folders

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 (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).

.../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 .m extension.

You must use a class folder if you want to use more than one file for your class definition. 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. See Methods In Separate Files 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. 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.

See 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 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 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 PathFolder and FileFile Defines

1

fldr1/foo.m

Class foo

2

fldr2/foo.m

Function foo

3

fldr3/@foo/foo.m

Class foo

4

fldr4/@foo/bar.m

Method bar

5

fldr5/foo.m

Class foo

Here is the logic that MATLAB applies to determine which version of foo to call:

Class fldr1/foo.m takes precedence over the class fldr3/@foo because:

  • fldr1/foo.m is before fldr3/@foo on the path

Class fldr3/@foo takes precedence over function fldr2/foo.m because:

  • fldr3/@foo is a class in a class folder

  • 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:

  • fldr2/foo.m comes before class fldr5/foo.m on the path

  • 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.

Class fldr3/@foo takes precedence over class fldr4/@foo because

  • The method bar is not recognized as part of the foo class that is defined only by fldr3/@foo

If fldr3/@foo/foo.m does not contain a classdef keyword (that is, 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 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.

More About

Was this topic helpful?