subsasgn
- :
newval =subsasgn(val, idx, rhs)¶ Assign rhs into val at the indexed location and return the result.
This is the function form of an indexed-assignment statement. idx describes the target location the same way as for
subsref: a structure array whose ‘type’ field selects the operator for each step—"()","{}", or"."—and whose ‘subs’ field gives that step’s subscripts as the string":"or a cell array of index values. The updated copy is returned; val itself is not modified in place. Three arguments are required.The following example zeroes the first two columns of a 3-by-3 matrix:
val = magic (3); idx.type = "()"; idx.subs = {":", 1:2}; val = subsasgn (val, idx, 0) ⇒ [ 0 0 6 0 0 7 0 0 2 ]which is the function-call equivalent of
val(:, 1:2) = 0. Following the usual rule for indexed assignment, an empty[]right-hand side deletes the selected elements rather than storing an empty value.When idx is an empty structure array with these fields, no assignment is performed and rhs is returned. The
endkeyword has no meaning in the subscripts supplied here and cannot be used.