Main Content

Use of size and numel with Classes

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

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
numel(d)
ans =

    10
dsub = d(7:end);
size(dsub)
ans =

     1     4

The double class defines these behaviors, including parentheses indexing.

Subclasses Inherit Behavior

Unless the subclass explicitly overrides superclass behavior, subclasses behave like their superclasses. For example, SimpleDouble subclasses double and defines no properties:

classdef SimpleDouble < double
   methods
      function obj = SimpleDouble(data)
         if nargin == 0
            data = 0;
         end
         obj = obj@double(data);
      end
   end
end

Create an object and assign the values 1:10:

sd = SimpleDouble(1:10);

The size function returns the size of the superclass part:

size(sd)
ans =

     1    10

The numel function returns the number of elements in the superclass part:

numel(sd)
ans =

     10

Object arrays return the size of the superclass arrays:

size([sd;sd])
ans =

     2    10
numel([sd;sd])
ans =

     20

The SimpleDouble class inherits the indexing behavior of the double class:

sdsub = sd(7:end);
size(sdsub)
ans =

     1     4

Classes Not Derived from Built-In Classes

Consider a simple value class. This class does not inherit the array-like behaviors of the double class. For example:

classdef VerySimpleClass
   properties
      Value
   end
end

Create an object and assign a 10-element array to the Value property:

vs = VerySimpleClass;
vs.Value = 1:10;
size(vs)
ans =

     1     1
numel(vs)
ans =

     1
size([vs;vs])
ans =

     2     1
numel([vs;vs])
ans =

     2

vs is a scalar object. The Value property is an array of doubles:

size(vs.Value)
ans =

     1    10

Apply indexing expressions to the object property:

vssub = vs.Value(7:end);
size(vssub)
ans =

     1     4

The vs.Value property is an array of class double:

class(vs.Value)
ans =

double

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.

Other MATLAB functions use the values returned by these functions. If you change the way that size and numel behave, ensure that the values returned make sense for the intended use of your class.

Overload numArgumentsFromSubscript Instead of numel

If classes implement a numArgumentsFromSubscript method, MATLAB calls it instead of numel to determine the number of elements returned by indexed expressions that return comma-separated lists. For example, expressions such as:

A(1:2).Prop

Both subsref and subsasgn use numArgumentsFromSubscript:

  • subsrefnumArgumentsFromSubscript computes the number of expected outputs (nargout) returned subsref.

  • subsasgnnumArgumentsFromSubscript computes the number of expected inputs (nargin) that MATLAB assigns as a result of a call to subsasgn.

Subclasses of built-in classes always return scalar objects as a result of subscripted reference and always use scalar objects for subscripted assignment.

If you define a class in which nargout for subsref or nargin for subsasgn must be a specific value, then overload numArgumentsFromSubscript to return that value.

See Also