MATLAB Object-Oriented Programming

(Joyce) #1

every other array element. Once you assign a nonempty object to an array, all array
elements must be nonempty objects.


NoteA class constructor must avoid returning empty objects by default.


For example, using the SimpleValue defined in the “Initialize Object Arrays” on page 10-
5 section, create an empty array:


ary = SimpleValue.empty(5,0);
class(ary)


ans =


SimpleValue


ary is an array of class SimpleValue. However, it is an empty array:


ary(1)


Index exceeds matrix dimensions.


If you make an assignment to a property value, MATLAB calls the SimpleClass
constructor to grow the array to the require size:


ary(5).Value = 7;
ary(5).Value


ans =


7


ary(1).Value


ans =


[]


MATLAB populates array elements one through five with SimpleValue objects created
by calling the class constructor with no arguments. Then MATLAB assigns the property
value 7 to the object at ary(5).


Empty Arrays
Free download pdf