Main Content

Indexed Assignment

How Indexed Assignment Works

Object indexed assignments are in three forms — parentheses, braces, and dot-name:

A(I) = B
A{I} = B
A.name = B

Each of these statements results in a call by MATLAB® to the subsasgn method of class A, or a call to the built-in subsasgn function if the class of A does not implement a subsasgn method.

MATLAB passes three arguments to subsasgn and requires subsasgn to return the result of the assignment:

A = subsasgn(A,S,B)

The first argument, A, is the object being assigned the value in the third argument B.

The second argument is the indexing structure, substruct. S has two fields:

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

  • S.subs is a cell array or character array containing the actual indices or field name. A colon used as an index is passed in the cell array as the character ':'. Ranges specified using a colon (e.g., 2:5) are expanded to 2 3 4 5.

For example, the assignment statement:

A(2,3) = B;

generates a call to subsasgn:

A = subsasgn(A,S,B)

S contains:

S.type = '()'
S.subs = {2,3}

The built-in subsasgn:

  • Determines the class of A. If B is not the same class as A, then MATLAB tries to construct an object of the same class as A using B as an input argument. If this attempt fails, MATLAB returns an error.

  • If A and B are, or can be made, into the same class, then MATLAB assigns the value of B to the array element at row 2, column 3.

  • If A does not exist before you execute the assignment statement, then MATLAB initializes the five array elements that come before A(2,3) with default objects of class B.

Similarly, this expression

A{2,3} = B

Uses these values for S:

S.type ='{}'
S.subs = {2,3} 

The built-in subsasgn:

  • Assigns B to the cell array element at row 2, column 3.

  • If A does not exist before you execute the assignment statement, MATLAB initializes the five cells that come before A(2,3) with []. The result is a 2-by-3 cell array.

This expression:

A.Name = B

Calls A = subsasgn(A,S,B) where the struct S has these values:

S.type = '.'
S.subs = 'Name'

The built-in subsasgn:

  • Assigns B to the struct field Name.

  • If A does not exist before you execute the assignment statement, MATLAB creates a struct variable, A with field Name and assigns the value of B to this field location.

  • If struct A exists, but has no field Name, then MATLAB adds the field Name and assigns the value of B to the new field location.

  • If struct A exists and has a Name field, then MATLAB assigns the value of B to Name.

You can redefine all or some of these assignment behaviors by implementing a subsasgn method for your class.

Indexed Assignment to Objects

If A is an object, this expression:

A.Name = B

Calls A = subsasgn(A,S,B) where, S has these values:

S.type = '.'
S.subs = 'Name'

The default subsasgn:

  • Attempts to assign B to the Name property.

  • If the class of A does not have a Name property, MATLAB returns an error.

  • If the Name property has restricted access (private or protected), MATLAB determines if the assignment is allowed based on the context in which the assignment is made.

  • If the class of A defines a set method for property Name, MATLAB calls the set method.

  • MATLAB applies all other property attributes before determining whether to assigning B to the property Name.

Compound Indexed Assignments

These simple calls are combined for more complicated indexing expressions. In such cases, length(S) is the number of indexing levels. For example,

A(1,2).PropertyName(1:4) = B

calls subsasgn(A,S,B), where S is a 3-by-1 array of structures with the values:

S(1).type = '()'    S(2).type = '.'              S(3).type = '()'
S(1).subs = {1,2}   S(2).subs = 'PropertyName'   S(3).subs = {1:4}

Related Topics