MATLAB Object-Oriented Programming

(Joyce) #1

of methods from all class folders having the same name to define the class. This behavior
is no longer supported.


For backward compatibility, classes defined in class folders always take precedence over
functions and scripts having the same name. This precedence applies to functions and
scripts that come before these classes on the path.


Changing Path to Update Class Definition


Changing your MATLAB path can change the class definition file for a class (see path).
However, for classes that are defined in path folders (that is, not in class @ folders), you
must clear the class before MATLAB recognizes the new folder as the current class
definition.


Class Definitions in Class Folders


Suppose that you define two versions of a class named Foo in two folders, fldA and
fldB.


fldA/+FooPkg/@Foo/Foo.m
fldB/+FooPkg/@Foo/Foo.m


Add folder fldA to the top of the path.


addpath fldA


Create an instance of class FooPkg.Foo. MATLAB uses fldA/+FooPkg/@Foo/Foo.m as
the class definition.


a = FooPkg.Foo;


Change the current folder to fldB.


cd fldB


The current folder is always first on the path. Therefore, MATLAB finds fldB/+FooPkg/
@Foo/Foo.m as the definition for class FooPkg.Foo.


b = FooPkg.Foo;


MATLAB automatically updates the existing instance, a, to use the new class definition in
fldB.


Folders Containing Class Definitions
Free download pdf