MATLAB Object-Oriented Programming

(Joyce) #1
obj1 = mypack.MyFirstClass;
obj2 = mypack.MySecondClass(arg);

Invoke the myFcn function that is in the package mysubpack:

mypack.mysubpack.myFcn(arg1,arg2);

If mypack.MyFirstClass has a method called myFcn, call it like any method call on an
object:

obj = mypack.MyFirstClass;
myFcn(obj,arg);

If mypack.MyFirstClass has a property called MyProp, assign it using dot notation and
the object:

obj = mypack.MyFirstClass;
obj.MyProp = x;

Packages and the MATLAB Path


You cannot add package folders to the MATLAB path, but you must add the package
parent folder to the MATLAB path. Package members are not accessible if the package
parent folder is not on the MATLAB path, even if the package folder is the current folder.
Making the package folder the current folder is not sufficient to add the package parent
folder to the path.

Package members remain scoped to the package. Always refer to the package members
using the package name. Alternatively, import the package into the function in which you
call the package member, see “Import Classes” on page 6-30.

Package folders do not shadow other package folders that are positioned later on the
path, unlike classes, which do shadow other classes. If two or more packages have the
same name, MATLAB treats them all as one package. If redundantly named packages in
different path folders define the same function name, then MATLAB finds only one of
these functions.

Resolving Redundant Names

Suppose a package and a class have the same name. For example:

fldr_1/+foo
fldr_2/@foo/foo.m

6 Defining and Organizing Classes

Free download pdf