MATLAB Object-Oriented Programming

(Joyce) #1

Objects in Index Expressions


In this section...
“Objects Indexes” on page 17-37
“Ways to Implement Objects as Indices” on page 17-37
“subsindex Implementation” on page 17-37

Objects Indexes


MATLAB can use objects as indices in indexed expressions. The rules of array indexing
apply — indices must be positive integers. Therefore, MATLAB must be able to derive a
value from the object that is a positive integer for use in the indexed expression.

Indexed expressions like X(A), where A is an object, cause MATLAB to call the
subsindex function. However, if an indexing expression results in a call to an overloaded
subsref or subsasgn method defined by the class of X, then MATLAB does not call
subsindex.

Ways to Implement Objects as Indices


There are several ways to implement indexing of one object by another object, X(A):


  • Define a subsindex method in the class of A that converts A to an integer. MATLAB
    calls A's subsindex method to perform indexing operations when the class of X does
    not overload subsref or subsasgn.

  • If the class of X overloads subsref or subsasgn, these methods can call the
    subsindex method of A explicitly. The class of A must implement a subsindex
    method that returns an appropriate value.

  • If the class of X overloads subsref or subsasgn, these methods can contain code
    that determines an integer index value. In this case, the class of A does not implement
    a subsindex method.


subsindex Implementation


subsindex must return the value of the object as a zero-based integer index value in the
range 0 to prod(size(X))-1.

Objects in Index Expressions
Free download pdf