MATLAB Object-Oriented Programming

(Joyce) #1
The name of each class definition file must match the name of the class that is specified
with the classdef keyword. 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.

Suppose that you have three classes defined in a single folder:

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

To use these classes, add path_folder to your MATLAB path:

addpath path_folder

Using Class Folders


A class folder name always begins with the @ character followed by the class name for the
folder name. A class folder must be contained in a path folder, but the class folder is not
on the MATLAB path. Place the class definition file inside the class folder, which also can
contain separate method files. The class definition file must have the same name as the
class folder (without the @ character). The class definition (beginning with the classdef
keyword) 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.

Use a class folder when 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. Class files provide
the advantage that MATLAB can explicitly identify any file in the folder as a method of
that class. For more information, see “Changing Path to Update Class Definition” on page
6-21.

The base name of each file must be a valid MATLAB function name. Valid function names
begin with an alphabetic character and can contain letters, numbers, or underscores. For
more information, see “Methods in Separate Files” on page 9-11.

6 Defining and Organizing Classes

Free download pdf