MATLAB Object-Oriented Programming

(Joyce) #1
C = A(3,4);

MATLAB calls the built-in subsref function to determine how to interpret the statement.
Similarly, if you execute a statement that involves indexed assignment:

C(4) = 7;

MATLAB calls the built-in subsasgn function to determine how to interpret the
statement.

The MATLAB default subsref and subsasgn functions also work with user-defined
objects. For example, create an array of objects of the same class:

for k=1:3
objArray(k) = MyClass;
end

Referencing the second element in the object array, objArray, returns the object
constructed when k = 2:

D = objArray(2);
class(D)

ans =

MyClass

You can assign an object to an array of objects of the same class, or an uninitialized
variable:

newArray(3,4) = D;

Arrays of objects behave much like numeric arrays in MATLAB. You do not need to
implement any special methods to provide standard array behavior with your class.

For general information about array indexing, see “Array Indexing”.

What You Can Modify


You can modify your class indexed reference and/or assignment behavior by implementing
class methods called subsref and subsasgn. For syntax description, see their respective
reference pages.

17 Specialize Object Behavior

Free download pdf