Main Content

subsref

Subscripted reference

Description

For classes authored in R2021b and later, the recommended process for customizing indexing is to inherit from some combination ofmatlab.mixin.indexing.RedefinesParen,matlab.mixin.indexing.RedefinesDot, andmatlab.mixin.indexing.RedefinesBrace. For more information, seeCustomize Object Indexing.

example

B= subsref(A,S)is called by MATLAB®for the syntaxA(i),A{i}, orA.iwhenAis an object.

Examples

collapse all

This example shows how MATLAB® callssubsreffor the following indexing expression.

A = magic(5); A(1:2,:)
ans =2×517 24 1 8 15 23 5 7 14 16

The syntax,A(1:2,:), results in a call toB = subsref(A,S)whereSis a 1-by-1 structure whereS.type is '()'andS.subs is {1:2,':'}. The colon character indicates a colon used as a subscript.

This example shows how MATLAB® callssubsref对索引表达式use braces.

C = {"one", 2,'three'}; C{1:2}
ans = "one"
ans = 2

The syntax, C{1:2}, results in a call to[c1,c2] = subsref(C,S)whereS.typeis'{}'andS.subsis{[1 2]}.

This example shows how MATLAB® callssubsref对索引表达式use dot notation.

A = struct('number',10); A.number
ans = 10

The syntaxA.numberresults in a call toB = subsref(A,S)whereS.Typeis'.'andS.subsis'number'.

Input Arguments

collapse all

Indexed object array, passed by MATLAB as the object array that is part of the indexing expression.

Indexing structure, passed by MATLAB as the indexingsubstructfor the indexing expression that caused the call to subsref. This structure has these fields:

  • type– Character vector or string scalar containing(),{}, or., specifying the subscript type.

  • subs– Cell array, character vector, or string scalar containing the actual subscripts.

Index expressions can use more than one level to form more complicated expressions. For exampleA{1}.field(3:5)has three levels of indexing. For this expression,Sis a 3-by-1 structure array with these fields:

disp(S(1)) type: '{}' subs: {[1]} disp(S(2)) type: '.' subs: 'field' disp(S(3)) type: '()' subs: {[3 4 5]}

Data Types:struct

Output Arguments

collapse all

Result of indexing expression.

More About

collapse all

Understanding Indexing Expressions

A(I)is an array formed from the elements ofAspecified by the subscript vectorI. The resulting array is the same size asIexcept for the special case whereAandIare both vectors. In this case,A(I)has the same number of elements asIbut has the orientation ofA.

A(I,J)是一个数组的元素形成rectan吗gular submatrix ofA, specified by the subscript vectorsIandJ. The resulting array haslength(I)rows andlength(J)columns. A colon used as a subscript indicates all elements in that dimension. For example,A(I,:)means all columns of those rows specified by vectorI. Similarly,A(:,J)means all rows of columns specified byJ.

A(I,J,K,...)is the array specified by the subscripts. The result islength(I)-by-length(J)-by-length(K)....

A{I}whereAis a cell array andIis a scalar forms a copy of the array in the specified cell ofA. IfIhas more than one element, this expression is a comma-separated list. You can also use multiple subscripts that specify a scalar element, as inA{3,4}.

A(I).fieldwhenAis a structure array andIis a scalar forms a copy of the array in the field with the namefield. IfIhas more than one element, this expression is a comma-separated list. IfAis a 1-by-1 structure array, then the subscript can be dropped. In this case,A.fieldis the same asA(1).field.

Extended Capabilities

Version History

Introduced before R2006a
>