This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Number of Arguments for subsref and subsasgn

How MATLAB Determines Number of Arguments

MATLAB® calls subsref or subsasgn to determine the result of executing code that involves indexed reference or assignment. The number of elements referenced or assigned by an indexing operation determines the number of arguments MATLAB uses to call subsref and subsasgn. That is, the indexing code determines the number of arguments that MATLAB:

  • Returns from the call to subsref

  • Passes to the call to subsasgn

Therefore, the indexing code determines the value of nargout for the call to subsref and the value of nargin for the call to subsasgn.

For example, consider the ValuesArray class.

classdef ValuesArray
      function obj = ValuesArray(v)
         if nargin > 0
            obj.Values = v;

Create an array of 10 ValuesArray objects.

l = ValuesArray.empty;
for k = 1:10
   l(k) = ValuesArray(k);

This subscripted reference returns a comma-separated list of three elements. For this statement, the value of nargout in subsref is 3.

ans =


ans =


ans =


The left side of a subscripted assignment statement affects the number of input arguments that MATLAB uses to call subsasgn. This subscripted assignment assigns three values to the three elements added to the array. For this assignment, the value of nargin within subsasgn is 5 (the object, the indexing substructure, and the three values to assign).

[l(11:13).Values] = l(1:3).Values
l = 

  1x13 ValuesArray array with properties:


If the number of right-side arguments cannot satisfy the number of left-side arguments, MATLAB returns an error:

[l(11:13).Values] = l(1).Values
Insufficient number of outputs from right hand side of equal sign to satisfy

Syntax for subsref, and subsasgn Methods

If a class overloads subsref to support either '{}', '.', or both types of indexing, and the operation returns more than one value, overload subsref to return multiple values using varargout:

function varargout = subsref(A,S)

If a class overloads subsasgn to support either '{}', '.', or both types of indexing, and the operation assigns more than one value, overload subsasgn to accept multiple values using varargin:

function A = subsagn(A,S,varargin)

Related Topics

Was this topic helpful?