This is machine translation

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


Redefine subscripted assignment


A = subsasgn(A,S,B)


A = subsasgn(A,S,B) called by MATLAB® for the syntax 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. Modify the indexed assignment behavior of classes by overloading subsasgn in the class.

    Note:   You must call subsasgn with an output argument. subsasgn does not modify the object used in the indexing operation (the first input argument). You must assign the output to obtain a modified object.

Input Arguments


Object used in indexing operation


Structure with two fields, type and subs. For compound indexing expressions, S is an array of structures, one for each level of indexing.

  • type is a char vector containing '()', '{}', or '.', indicating the type of indexing used.

  • subs is a cell array or character array containing the actual subscripts.


Value being assigned (right side of assignment statement)

Output Arguments


Result of the assignment statement, which is the modified object passed in as the first argument.

If your implementation of a subsasgn method assigns more than one value, use varargin for the third input argument. For more information, see Syntax for subsref, and subsasgn Methods.


Argument values for the subsasgn for the expression shown:

A(1:2,:) = B;

The syntax A(1:2,:) = B calls A = subsasgn(A,S,B) where S is a structure with S.type = '()' and S.subs = {1:2,':'}. The colon character (':') indicates a colon used as a subscript.

For the expression:

A{1:2} = B;

The syntax A{1:2} = B calls A = subsasgn(A,S,B) where S.type = '{}' and S.subs = {[1 2]}.

For the expression:

A.field = B;

The syntax A.field = B calls A = subsasgn(A,S,B) where S.type = '.' and S.subs = 'field'.

For the expression:

A(1,2).name(3:5) = B;

Simple calls combine in a straightforward way for more complicated indexing 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 array of structures 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 4 5]}

Related Examples

More About

collapse all


Within the subsasgn method defined by a class, MATLAB calls the built-in subsasgn. Calling the built-in enables you to use the default indexing behavior when defining specialized indexing. For more information, see Built-In subsref and subsasgn Called in Methods.


In the assignment A(J,K,...) = B(M,N,...), subscripts J, K, M, N, and so on, can be scalar, vector, or arrays, when all the following are true:

  • The number of subscripts specified for B, excluding trailing subscripts equal to 1, does not exceed the value returned by 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.

Introduced before R2006a

Was this topic helpful?