MATLAB Function Reference |

Overloaded method for `A(I)=B`

, `A{I}=B`

, and `A.field=B`

**Syntax**

**Description**

```
A = subsasgn(A, S, B)
```

is called for the syntax `A(i)=B`

, `A{i}=B`

, or `A.i=B`

when `A`

is an object. `S`

is a structure array with the fields

`type`

: A string containing`'()'`

,`'{}'`

, or`'.'`

, where`'()'`

specifies integer subscripts,`'{}'`

specifies cell array subscripts, and`'.'`

specifies subscripted structure fields.`subs`

: A cell array or string containing the actual subscripts.

**Remarks**

`subsasgn`

is designed to be used by the MATLAB interpreter to handle indexed assignments to objects. Calling `subsasgn`

directly as a function is not recommended. If you do use `subsasgn`

in this way, it conforms to the formal MATLAB dispatching rules and can yield unexpected results.

In the assignment `A(J,K,...) = B(M,N,...)`

, subscripts `J`

, `K`

, `M`

, `N`

, etc. may be scalar, vector, or array, provided that all of the following are true:

- The number of subscripts specified for
`B`

, excluding trailing subscripts equal to 1, does not exceed`ndims`

`(B)`

. - The number of nonscalar subscripts specified for
`A`

equals the number of nonscalar subscripts specified for`B`

. For example,`A(5, 1:4, 1, 2) = B(5:8)`

is valid because both sides of the equation use one nonscalar subscript. - The order and length of all nonscalar subscripts specified for
`A`

matches the order and length of nonscalar subscripts specified for`B`

. For example,`A(1:4, 3, 3:9) = B(5:8, 1:7)`

is valid because both sides of the equation (ignoring the one scalar subscript`3`

) use a 4-element subscript followed by a 7-element subscript.

If `A`

is an array of one of the fundamental MATLAB data types, then assigning a value to `A`

with indexed assignment calls the builtin MATLAB `subsasgn`

method. It does not call any `subsasgn`

method that you may have overloaded for that data type. For example, if `A`

is an array of type `double`

, and there is an `@double/subsasgn`

method on your MATLAB path, the statement `A(I) = B`

does not call this method, but calls the MATLAB builtin `subsasgn`

method instead.

**Examples**

The syntax `A(1:2,:)=B`

calls `A=subsasgn(A,S,B)`

where `S`

is a 1-by-1 structure with `S.type='()'`

and `S.subs = {1:2,':'}`

. A colon used as a subscript is passed as the string `':'`

.

The syntax `A{1:2}=B`

calls `A=subsasgn(A,S,B)`

where `S.type='{}'`

.

The syntax `A.field=B`

calls `subsasgn(A,S,B)`

where` S.type='.'`

and `S.subs='field'`

.

These simple calls are combined in a straightforward way for more complicated subscripting expressions. In such cases `length(S)`

is the number of subscripting levels. For instance, `A(1,2).name(3:5)=B`

calls `A=subsasgn(A,S,B)`

where `S`

is a 3-by-1 structure array with the following values:

`S(1).type='()'` |
`S(2).type='.'` |
`S(3).type='()'` |

`S(1).subs={1,2}` |
`S(2).subs='name'` |
`S(3).subs={3:5}` |

**See Also **

See Handling Subscripted Assignment for more information about overloaded methods and `subsasgn`

.

subplot | subsindex |

© 1994-2005 The MathWorks, Inc.