MATLAB Object-Oriented Programming

(Joyce) #1
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:


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

  • subsasgn — numArgumentsFromSubscript 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
numArgumentsFromSubscript

Related Examples



  • “Modify nargout and nargin for Indexing Methods” on page 17-9


12 How to Build on Other Classes

Free download pdf