MATLAB Object-Oriented Programming

(Joyce) #1

Use of size and numel with Classes


In this section...
“size and numel” on page 12-77
“Built-In Class Behavior” on page 12-77
“Subclasses Inherit Behavior” on page 12-78
“Classes Not Derived from Built-In Classes” on page 12-79
“Change the Behavior of size or numel” on page 12-81
“Overload numArgumentsFromSubscript Instead of numel” on page 12-82

size and numel


The size function returns the dimensions of an array. The numel function returns the
number of elements in an array, which is equivalent to prod(size(objArray)). That is,
the product of the array dimensions.

The size and numel functions work consistently with arrays of user-defined objects.
There is generally no need to overload size or numel in user-defined classes.

Several MATLAB functions use size and numel to perform their operations. Therefore, if
you do overload either of these functions in your class, be sure that objects of your class
work as designed with other MATLAB functions.

If your class modifies array indexing, see “Overload numArgumentsFromSubscript
Instead of numel” on page 12-82

Built-In Class Behavior


When you use the size and numel functions in classes derived from built-in classes,
these functions behave the same as they behave in the superclass.

Consider the built-in class double:

d = 1:10;
size(d)

ans =

1 10

Use of size and numel with Classes
Free download pdf