Skip to Main Content Skip to Search
Product Documentation

Organizing Classes in Folders

Options for Class Folders

There are two types of folders that can contain class definitions. Each behave differently in a number of respects.

See the path function for information about the MATLAB path.

@-Folders

An @-folder is contained by a path folder, but is not itself on the MATLAB path. Place the class definition file inside the @-folder, which can also contain method files. The class definition file must have the same name as the @-folder (without the @-sign) 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). The name of the class must match the name of the file that contains the class definition.

You must use an @-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. All files have a .m extension.

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 @-folder for each class. However, the entire class definition must be contained within a single file. All files have a .m extension.

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 for more information). 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 @ 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 an @-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:

Previous Behavior of Classes Defined in @-Folders

In MATLAB Versions 5 through 7, @-folders do not shadow other @-folders having the same name, but residing in later path folders. Instead, the class is defined by the combination of methods from all @-folders having the same name. This is no longer true.

Note that for backward compatibility, classes defined in @-folders always take precedence over functions and scripts having the same name, even those that come before them on the path.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS