## 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 properties Values end methods function obj = ValuesArray(v) if nargin > 0 obj.Values = v; end end end end```

Create an array of 10 `ValuesArray` objects.

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

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

`l(1:3).Values`
```ans = 1 ans = 2 ans = 3```

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: Values```

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 assignment.```

### 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) ... end```

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 = subsasgn(A,S,varargin) ... end```