Programming Previous page   Next Page

Setting Up Class Directories

The M-files defining the methods for a class are collected together in a directory referred to as the class directory. The directory name is formed with the class name preceded by the character @. For example, one of the examples used in this chapter is a class involving polynomials in a single variable. The name of the class, and the name of the class constructor, is polynom. The M-files defining a polynomial class would be located in directory with the name @polynom.

The class directories are subdirectories of directories on the MATLAB search path, but are not themselves on the path. For instance, the new @polynom directory could be a subdirectory of the MATLAB working directory or your own personal directory that has been added to the search path.

Adding the Class Directory to the MATLAB Path

After creating the class directory, you need to update the MATLAB path so that MATLAB can locate the class source files. The class directory should not be directly on the MATLAB path. Instead, you should add the parent directory to the MATLAB path. For example, if the @polynom class directory is located at

you add the class directory to the MATLAB path with the addpath command

Using Multiple Class Directories

A MATLAB class can access methods in multiple @classname directories if all such directories are visible to MATLAB (i.e., the parent directories are on the MATLAB path or in the current directory). When you attempt to use a method of the class, MATLAB searches all the visible directories named @classname for the appropriate method.

For more information, see How MATLAB Determines Which Method to Call.

Data Structure

One of the first steps in the design of a new class is the choice of the data structure to be used by the class. Objects are stored in MATLAB structures. The fields of the structure, and the details of operations on the fields, are visible only within the methods for the class. The design of the appropriate data structure can affect the performance of the code.


Previous page  Debugging Class Methods Tips for C++ and Java Programmers Next page

© 1994-2005 The MathWorks, Inc.