Main Content

Subscripted assignment

is called for the syntax `A`

= subsasgn(`A`

,`S`

,`B`

)`A(i) = B`

, `A{i} = B`

, or
`A.i = B`

when `A`

is an object.

MATLAB^{®} uses the built-in `subsasgn`

function to interpret indexed
assignment statements:

`A(i) = B`

assigns the values of`B`

into the elements of`A`

specified by the subscript vector`i`

.`B`

must have the same number of elements as`i`

or be a scalar value.`A(i,j) = B`

assigns the values of`B`

into the elements of the rectangular submatrix of`A`

specified by the subscript vectors`i`

and`j`

.`B`

must have`length(i)`

rows and`length(j)`

columns.A colon used as a subscript, as in

`A(i,:) = B`

or`A(:,i) = B`

, indicates the entire column or row.For multidimensional arrays,

`A(i,j,k,…) = B`

assigns`B`

to the specified elements of`A`

.`B`

must be`length(i)`

-by-`length(j)`

-by-`length(k)`

-… or be shiftable to that size by adding or removing singleton dimensions.

**Tip**

You can use fixed-point assignment, for example, `A(:) = B`

, to
cast a value with one numeric type into another numeric type. This subscripted
assignment statement assigns the value of `B`

into `A`

while keeping the numeric type of `A`

. Subscripted assignment works the
same way for integer data types.

**Note**

You must call `subsasgn`

with an output argument.
`subsasgn`

does not modify the object used in the indexing
operation (the first argument). You must assign the output to obtain a modified
object.