Main Content

subsasgn

Subscripted assignment using function call

Description

R = subsasgn(A,S,B) returns the result from a parentheses, brace, or dot indexing assignment, or a combination of one or more of those types, performed on the array A. The structure S contains the details of the assignment to be performed, and B is the value to be assigned into A.

Note

Performing an indexing assignment by calling the subsasgn function explicitly is always slower than the equivalent indexing statement.

example

Examples

collapse all

Create a 3-by-3 matrix and a 1-by-3 vector. Assign the vector to the first row of the matrix using standard indexing syntax.

A = magic(3);
B = [-1 -1 -1];
A(1,:) = B
A =

    -1    -1    -1
     3     5     7
     4     9     2

Call subsasgn to perform the operation equivalent to A(1,:) = B, with B = [-2 -2 -2]. Use the substruct function to create the structure that describes the indexing expression.

B = [-2 -2 -2];
A = subsasgn(A,substruct('()',{1,':'}),B)
A =

    -2    -2    -2
     3     5     7
     4     9     2

Create a structure with one field that has a vector value. Replace the second element of the vector using standard indexing syntax.

A.data = [5 10 15];
B = 20;
A.data(2) = B
A = 

  struct with fields:

    data: [5 20 15]

Call subsasgn to perform the operation equivalent to A.data(2) = 25. Use the substruct function to create the structure that describes the compound indexing expression.

A = subsasgn(A,substruct('.','data','()',{2}),25)
A = 

  struct with fields:

    data: [5 25 15]

Create a 1-by-3 cell array and replace the third element using standard indexing syntax.

C = {"one",2,'three'};
C{3} = 'four'
C =

  1×3 cell array

    {["one"]}    {[2]}    {'four'}

Call subsasgn to perform the operation equivalent to C{3} = 'five'. Use the substruct function to create the structure that describes the indexing expression.

C = subsasgn(C,substruct('{}',{3}),'five')
C =

  1×3 cell array

    {["one"]}    {[2]}    {'five'}}

Input Arguments

collapse all

Target of indexing expression, specified as an array of any data type.

Indexing argument structure, also referred to as the substruct, specified as a structure array. Each structure has two fields:

  • type — Type of indexing expression, specified as "()", ".", or "{}" to indicate parentheses, dot, or brace indexing, respectively. The value can be a character vector or string scalar.

  • subs — Subscript values, specified as a character vector or string for dot indexing or a cell array of index vectors for parentheses or brace indexing.

S is a scalar structure for single indexing expressions, like A{1}, or a structure array for compound indexing expressions. For example, A{1}.field(3:5) has three levels of indexing. For this expression, S is a 1-by-3 structure array:

S(1)

type: '{}'

subs: {[1]}

S(2)

type: '.'

subs: 'field'

S(3)

type: '()'

subs: {[3 4 5]}

Data Types: struct

Value assigned to the indexing target, specified as an array of any data type.

Output Arguments

collapse all

Result of the subscripted assignment, returned as an array.

You must call subsasgn with an output argument. subsasgn does not modify the indexing target (A), so you must define an output variable for the modified array.

Tips

Extended Capabilities

expand all

Version History

Introduced before R2006a