| MATLAB® | ![]() |
| On this page… |
|---|
There are two types of directories that can contain class definitions. Each behave differently in a number of respects.
@-directories — Directory name begins with "@" and is not on the MATLAB® path, but its parent directory is on the path. Use this type of directory when you want to use multiple files for one class definition. There can be only one class per directory and the name of the class must match the name of the directory, without the "@" symbol.
path directories — Directory name does not use @ character and is itself on the MATLAB path. Use this type of directory when you want multiple classes in one directory.
See the path function for information about the MATLAB path.
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.
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.
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:
Class dir1/foo.m takes precedence over the class dir3/@foo because it is before dir3/@foo on the path.
Class dir3/@foo takes precedence over function dir2/foo.m because it is a class in an @-directory and dir2/foo.m is not a class (@-directory classes take precedence over functions).
Function dir2/foo.m takes precedence over class dir5/foo.m because it comes before class dir5/foo.m on the path and because class dir5/foo.m is not in an @-directory. Classes not defined in @-directories abide by path order with respect to functions.
Class dir3/@foo takes precedence over class dir4/@foo; therefore, the method bar is not recognized as part of the foo class (which is defined only by dir3/@foo).
If dir3/@foo/foo.m does not contain a classdef keyword (i.e., it is a MATLAB class prior to Version 7.6), then dir4/@foo/bar.m becomes a method of the foo class defined in dir3/@foo.
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.
![]() | Class Attributes | Specifying Class Precedence | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |