Accelerating the pace of engineering and science

Documentation Center

• Trial Software

subsasgn

Subscripted assignment

Syntax

A = subsasgn(A, S, B)

Description

A = subsasgn(A, S, B) is 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.

If A is a fundamental class (see Fundamental MATLAB Classes), then an indexed reference to A calls the built-in subsasgn function. It does not call a subsasgn method that you have overloaded for that class. Therefore, if A is an array of class double, and there is an @double/subsasgn method on your MATLAB path, the statement A(I) = B calls the MATLAB built-in subsasgn function.

Input Arguments

 A Object
 S struct array with two fields, type and subs. type is a string containing '()', '{}', or '.', where '()' specifies integer subscripts, '{}' specifies cell array subscripts, and '.' specifies subscripted structure fields.subs is a cell array or string containing the actual subscripts.
 B Assignment value (right-hand side)

Output Arguments

 A Result of evaluating assignment.

Examples

See how MATLAB calls subsasgn for the expression:

`A(1:2,:) = B;`

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,':'}. The string ':' indicates a colon used as a subscript.

See how MATLAB calls subsasgn 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]}.

See how MATLAB calls subsasgn for the expression:

`A.field = B;`

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

See how MATLAB calls subsasgn 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 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 4 5]}

expand all

Tips

Within a class's own methods, MATLAB calls the built-in subsasgn, not the class defined subsasgn. This behavior enables to use the default subsasgn behavior when defining specialized indexing for your class. See subsref and subsasgn Within Class Methods — Built-In Called for more information.

Algorithms

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.

See numel for information concerning the use of numel with regards to the overloaded subsasgn function.