MATLAB Object-Oriented Programming

(Joyce) #1

Methods in Separate Files


In this section...
“Class Folders” on page 9-11
“Define Method in Function File” on page 9-12
“Specify Method Attributes in classdef File” on page 9-12
“Methods You Must Define in the classdef File” on page 9-13

Class Folders


You can define class methods in files that are separate from the class definition file, with
certain exceptions (see “Methods You Must Define in the classdef File” on page 9-13).

To use multiple files for class definitions, put the class files in a folder having a name
beginning with the @ character followed by the name of the class (this is called a class
folder). Ensure that the parent folder of the class folder is on the MATLAB path.

If the class folder is contained in one or more package folders, then the top-level package
folder must be on the MATLAB path.

For example, the folder @MyClass must contain the file MyClass.m (which contains the
classdef block) and can contain other methods and function defined in files having a .m
extension. The folder @MyClass can contain a number of files:

@MyClass/MyClass.m
@MyClass/subsref.m
@MyClass/subsasgn.m
@MyClass/horzcat.m
@MyClass/vertcat.m
@MyClass/myFunc.m

NoteMATLAB treats any.m file in the class folder as a method of the class. The base
name of the file must be a valid MATLAB function name. Valid function names begin with
an alphabetic character, and can contain letters, numbers, or underscores.

Methods in Separate Files............................ 9-

Free download pdf