MATLAB Object-Oriented Programming

(Joyce) #1

The indexing structure contains this information in the type and subs fields.


Values of the Indexing Structure


When executing an indexing expression, MATLAB calls the class subsref or subsasgn
method, if the class overloads these functions. One of the arguments passed to the
method is the indexing structure. The indexing structure has two fields:



  • type — One of the three possible indexing types: '.', '()', '{}'

  • subs — A char vector with the property name or cell array of the indices used in the
    expression, including : and end.


If the indexing expression is a compound expression, then MATLAB passes an array of
structures, one struct for each level of indexing. For example, in this expression:


a.DataArray(1,:)


the indexing structure array S has these values:



  • S(1).type is set to '.', indicating that the first indexing operation is a dot.

  • s(1).subs is set to the property name, 'DataArray'


The second level of indexing is in the second element of the indexing structure:



  • S(2).types is set to '()' indicating the second indexing operation is parentheses
    indexing

  • S(2).subs is set to a cell array containing the indices {[1],[:]}


Typical Patterns for Indexing Methods


To overload the subsref and subasgn functions:



  • Determine the full indexing expression using the types and subs fields of the
    indexing structure.

  • Implement the specialized behaviors for the indexing operations supported by the
    class.

  • Return the appropriate values or modified objects in response to the call by MATLAB.


A switch statement is a convenient way to detect the first level of indexing. There are
three types of indexing—dot, parentheses, and braces. Each case block in the switch


Code Patterns for subsref and subsasgn Methods
Free download pdf