This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Redefine subscripted reference for objects


B = subsref(A,S)


B = subsref(A,S) is called by MATLAB® for the syntax A(i), A{i}, or A.i when A is an object.

MATLAB uses the built-in subsref function to interpret indexed references to objects. To modify the indexed reference behavior of objects, overload subsref in the class.

Input Arguments


Object used in indexing operation


Structure with two fields, type and subs. For compound indexing expressions, S is an array of structures, one for each level of indexing.

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

  • subs is a cell array or character array containing the actual subscripts.

Output Arguments


Result of the indexed expression.

If your implementation of a subsref method returns either no output or more than one output, use varargout for the output argument. For more information, see Syntax for subsref, and subsasgn Methods.


See how MATLAB calls subsref for the expression:


The syntax A(1:2,:) calls B = subsref(A,S) where S is a 1-by-1 structure with S.type='()' and S.subs={1:2,':'}. The colon character ':' indicates a colon used as a subscript.

See how MATLAB calls subsref for the expression:


The syntax A{1:2} calls B = subsref(A,S) where S.type='{}' and S.subs={[1 2]}.

See how MATLAB calls subsref for the expression:


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

See how MATLAB calls subsref for the expression:


Simple calls combine in a straightforward way for more complicated indexing expressions. In such cases, length(S) is the number of subscript levels. For instance, A(1,2).name(3:5) calls subsref(A,S) where S is a 3-by-1 structure array with the following values:

S(1).subs={1,2}S(2).subs='name'S(3).subs={[3 4 5]}

Related Examples

More About

collapse all


Within the subsref method defined by a class, MATLAB calls the built-in subsref. Calling the built-in enables you to use the default indexing behavior when defining specialized indexing. For more information, see Built-In subsref and subsasgn Called in Methods.

Introduced before R2006a

Was this topic helpful?