MATLAB Object-Oriented Programming

(Joyce) #1

Modify nargout and nargin for Indexing Methods


In this section...
“When to Modify Number of Arguments” on page 17-9
“How to Modify Number of Arguments” on page 17-9

When to Modify Number of Arguments


By default, the number of values referenced by an indexing operation determines how
many output arguments MATLAB uses to call subsref. Similarly, the number of values to
assign in an indexed assignment operation determines how many input arguments
MATLAB uses to call subsasgn.

If your class design requires that indexing operations return or assign a different number
of values than the number defined by the indexing operation, use
numArgumentsFromSubscript to specify the required number.
numArgumentsFromSubscript provides control over nargout for subsref and
nargin for subsasgn.

If your class uses numArgumentsFromSubscript, implement subsref and subsasgn
methods to define the actual values returned or assigned by indexing operations.

Before MATLAB release R2015b, MATLAB produced different results for some indexing
expressions that return or assign to a comma-separated list. Use
numArgumentsFromSubscript to support code that relies on the behavior of previous
releases. Also, now you can overload numArgumentsFromSubscript instead of numel
to achieve specific results without redefining how numel works.

How to Modify Number of Arguments


When a class overloads numArgumentsFromSubscript, MATLAB calls this method
instead of numel to compute the number of arguments expected for subsref nargout
and subsasgn nargin.

If classes do not overload numArgumentsFromSubscript, MATLAB calls numel to
compute the values of nargout or nargin.

MATLAB calls numArgumentsFromSubscript with three input arguments:

Modify nargout and nargin for Indexing Methods
Free download pdf