subsasgn - Subscripted assignment using function call - MATLAB (original) (raw)

Subscripted assignment using function call

Syntax

Description

[R](#mw%5F643c1a7e-db57-4e67-aeb4-b33919dc64e1) = subsasgn([A](#mw%5F077c13be-24f3-4e6f-9985-14a222a01b44),[S](#mw%5Fbcd09fa2-da5e-4412-91ec-c5e2af581f29),[B](#mw%5F94ea0986-167d-442a-abad-2605a0c938d3)) 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 structureS contains the details of the assignment to be performed, andB 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

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

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

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 toA.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 toC{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:

S is a scalar structure for single indexing expressions, likeA{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

The subsasgn function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a