MATLAB Object-Oriented Programming

(Joyce) #1

Indexed Reference


In this section...
“How Indexed Reference Works” on page 17-28
“Compound Indexed References” on page 17-29

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,':'}

17 Specialize Object Behavior

Free download pdf