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 subsref
function if the class of
A
does not implement a subsref
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'
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}