Organizing Classes in Directories

Options for Class Directory

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

See the path function for information about the MATLAB path.

@-Directories

An @-directory is contained by a path directory, but is not itself on the MATLAB path. You place the class definition file inside the @-directory, which can also contain method files. The class definition file must have the same name as the @-directory (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 @-directory 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.

Path Directories

You can locate class definition files in directories that are on the MATLAB path. These classes are visible on the path like any ordinary function. Class definitions placed in path directories 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 directory eliminates the need to create a separate @-directory for each class. However, the entire class definition must be contained within a single file.

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 @-directory 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 directories, containing the files indicated:

dir1/foo.m         % defines class foo
dir2/foo.m         % defines function foo
dir3/@foo/foo.m    % defines class foo
dir4/@foo/bar.m    % defines method bar
dir5/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 @-Directories

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

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

  


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