34.3.1 Defining Indexing And Indexed Assignment

Objects can be indexed with parentheses, either like
a (idx) or like a {idx}, or even
like a (idx).field. However, it is up to the user
to decide what this indexing actually means. In the case of our polynomial
class p (n) might mean either the coefficient of the
n-th power of the polynomial, or it might be the evaluation of the
polynomial at n. The meaning of this subscripted referencing is
determined by the subsref method.

Built-in Function: subsref(val, idx)

Perform the subscripted element selection operation according to the
subscript specified by idx.

The subscript idx is expected to be a structure array with fields
‘type’ and ‘subs’. Valid values for ‘type’ are
‘"()"’, ‘"{}"’, and ‘"."’. The ‘subs’ field may
be either ‘":"’ or a cell array of index values.

The following example shows how to extract the first two columns of a matrix

For example we might decide that indexing with "()" evaluates the
polynomial and indexing with "{}" returns the n-th coefficient
(of n-th power). In this case the subsref method of our
polynomial class might look like

The equivalent functionality for subscripted assignments uses the
subsasgn method.

Built-in Function: subsasgn(val, idx, rhs)

Perform the subscripted assignment operation according to the subscript
specified by idx.

The subscript idx is expected to be a structure array with fields
‘type’ and ‘subs’. Valid values for ‘type’ are
‘"()"’, ‘"{}"’, and ‘"."’. The ‘subs’ field may
be either ‘":"’ or a cell array of index values.

The following example shows how to set the two first columns of a 3-by-3
matrix to zero.

Query or set the internal flag for subsasgn method call optimizations.

If true, Octave will attempt to eliminate the redundant copying when calling
the subsasgn method of a user-defined class.

When called from inside a function with the "local" option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.

Note that the subsref and subsasgn methods always receive the
whole index chain, while they usually handle only the first element. It is the
responsibility of these methods to handle the rest of the chain (if needed),
usually by forwarding it again to subsref or subsasgn.

If you wish to use the end keyword in subscripted expressions
of an object, then the user needs to define the end method for
the class. For example, the end method for our polynomial class might
look like

which is a fairly generic end method that has a behavior similar to
the end keyword for Octave Array classes. It can then be used as
follows:

p = polynomial ([1,2,3,4]);
p(end-1)
⇒ 3

Objects can also be used as the index in a subscripted expression themselves
and this is controlled with the subsindex function.

Function File: idx =subsindex(a)

Convert an object to an index vector.

When a is a class object defined with a class constructor, then
subsindex is the overloading method that allows the conversion of
this class object to a valid indexing vector. It is important to note that
subsindex must return a zero-based real integer vector of the class
"double". For example, if the class constructor