Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Indexed Reference

How Indexed Reference Works

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

A(I)
A{I}
A.name

Each of these statements results in a call by MATLAB® to the subsref 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 two arguments to subsref and requires subsref to return the result of the indexed reference:

B = subsref(A,S)

The first argument is the object being referenced, A. The second argument, S, is a substruct with two fields:

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

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

For example, the expression:

A(1:4,:)

Causes MATLAB to call subsref(A,S), where S is a 1-by-1 structure with a two-element cell array. The cell array contains the numbers 1, 2, 3, 4, and the colon character :.

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

Returning the contents of each cell of S.subs gives the index values for the first dimension and a char vector ':' for the second dimension:

S.subs{:}
ans =

     1     2     3     4

ans =

:

The default subsref returns all array elements in rows 1 through 4 and all the columns in the array.

Similarly, this expression:

A{1:4}

Uses a cell array containing the numbers 1, 2, 3, 4.

S.type ='{}'
S.subs = {1:4} 

The default subsref returns the contents of all cell array elements in rows 1 through 4 and all the columns in the array.

This expression:

A.Name

Calls subsref(A,S), where the struct S has these values:

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

Compound Indexed References

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)

calls subsref(A,S), where S is a 3-by-1 array of structs 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}

How to Write subsref for Objects

Your class subsref method interprets the indexing expressions passed in by MATLAB. The subsref method implements any behavior your class to supports.

For example, use a switch statement to determine the type of indexing used and to obtain the actual indices. The following three code fragments illustrate how to interpret the input arguments. In each case, the case expression returns the value (B) that your subsref function returns.

For a parentheses index:

% Parse A(n)
switch S.type
   case '()'
      B = A(S.subs{:}); 
end

For a brace index when A is an object, you can implement indexing using {} as a special case. For example, suppose that you want A{n} to access a property of A called CellProperty, and that contains a cell array:

% Parse A{n}
switch S.type
   case '{}'
      B = A.CellProperty{S.subs{:}}; 
end

Although braces are used for cell arrays in MATLAB, your subsref method can define its own meaning for this syntax.

Dot name indexing typically accesses property values. The name can be an arbitrary char vector for which you take an arbitrary action:

switch S.type
   case '.'
      switch S.subs
         case 'name1'
            B = A.name1;
         case 'name2'
            B = A.name2;
      end
end

If the dot name is a method call, passing arguments requires a second level of indexing.

Use Default Indexing

The method can call the built-in subsref to handle any of the three indexing types that you do not want to change. For example, call the built-in for {} indexing:

case '{}'
varargout = builtin('subsref',A,s);

Related Topics

Was this topic helpful?