subsref
- :
newval =subsref(val, idx)¶ Index into val programmatically and return the selected result.
This is the function form of an indexing expression. idx describes the chain of indexing steps to apply, as a structure array whose ‘type’ field holds the operator for each step—
"()"for ordinary indexing,"{}"for cell-content indexing, or"."for field access—and whose ‘subs’ field holds that step’s subscripts, given either as the string":"for a whole dimension or as a cell array of index values. Successive elements of the array are applied left to right, so a multi-step idx expresses compound indexing in one call. Two arguments are required.The following example selects the first two columns of a matrix:
val = magic (3) ⇒ val = [ 8 1 6 3 5 7 4 9 2 ] idx.type = "()"; idx.subs = {":", 1:2}; subsref (val, idx) ⇒ [ 8 1 3 5 4 9 ]which is simply the function-call equivalent of
val(:, 1:2).When idx is an empty structure array with these fields, no indexing is performed and val is returned unchanged. The
endkeyword has no meaning in the subscripts supplied here and cannot be used.