MATLAB Object-Oriented Programming

(Joyce) #1

Create an array of VerySimpleClass objects:


vsArray(1:10) = VerySimpleClass;


The Value property for array elements 2 through 10 is empty:


isempty([vsArray(2:10).Value])


ans =


1


MATLAB does not apply scalar expansion to object array property value assignment. Use
the deal function for this purpose:


[vsArray.Value] = deal(1:10);
isempty([vsArray.Value])


ans =


0


The deal function assigns values to each Value property in the vsArray object array.


Indexing rules for object arrays are equivalent to the rules for arrays of struct:


vsArray(1).Value


ans =


1 2 3 4 5 6 7 8 9 10


vsArray(1).Value(6)


ans =


6


Change the Behavior of size or numel


Subclasses of built-in numeric classes inherit a size method, which operates on the
superclass part of the subclass object (these methods are hidden). If you want size or
numel to behave differently, override them by defining a size or numel method in your
subclass.


Use of size and numel with Classes
Free download pdf