Modify nargout and nargin for Indexing Methods

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:

function n = numArgumentsFromSubscript(obj,s,indexingContext)
   ...
end
Input ArgumentDescription

obj

Object whose subsref or subsasgn method is called

s

Indexing structure that contains the indexing type and indices used in the operation

indexingContext

Context in which the indexing operation occurs: indexed reference used as a statement, index reference used as a function argument, and indexed assignment

MATLAB uses the value returned by numArgumentsFromSubscript for indexed reference and assignment. Determine the context in which the indexing operation executes by testing the value of indexingContext in your implementation of numArgumentsFromSubscript. For example, test for any or all the possible indexing contexts.

function n = numArgumentsFromSubscript(obj,~,indexingContext)
   switch indexingContext
      case matlab.mixin.util.IndexingContext.Statement
         n = ...; % nargout for indexed reference used as statement
      case matlab.mixin.util.IndexingContext.Expression
         n = ...; % nargout for indexed reference used as function argument
      case matlab.mixin.util.IndexingContext.Assignment
         n = ...; % nargin for indexed assignment
   end
end

For more information and examples, see numArgumentsFromSubscript.

Note

For MATLAB version R2015b and later releases, overload numArgumentsFromSubscript instead of numel to customize indexing for your class.

Related Topics