Return a copy of the structure s with the field member field set to the value val.
For example:
s = struct (); s = setfield (s, "foo bar", 42);
This is equivalent to
s.("foo bar") = 42;
Note that ordinary structure syntax s.foo bar = 42
cannot be
used here, as the field name is not a valid Octave identifier because of
the space character. Using arbitrary strings for field names is
incompatible with MATLAB, and this usage will emit a warning if the
warning ID Octave:language-extension
is enabled.
See ‘XREFwarning_ids’.
With the second calling form, set a field of a structure array. The input sidx selects an element of the structure array, field specifies the field name of the selected element, and fidx selects which element of the field (in the case of an array or cell array). The sidx, field, and fidx inputs can be repeated to address nested structure array elements. The structure array index and field element index must be cell arrays while the field name must be a string.
For example:
s = struct ("baz", 42); setfield (s, {1}, "foo", {1}, "bar", 54) ⇒ ans = scalar structure containing the fields: baz = 42 foo = scalar structure containing the fields: bar = 54
The example begins with an ordinary scalar structure to which a nested
scalar structure is added. In all cases, if the structure index sidx
is not specified it defaults to 1 (scalar structure). Thus, the example
above could be written more concisely as
setfield (s, "foo", "bar", 54)
Finally, an example with nested structure arrays:
sa.foo = 1; sa = setfield (sa, {2}, "bar", {3}, "baz", {1, 4}, 5); sa(2).bar(3) ⇒ ans = scalar structure containing the fields: baz = 0 0 0 5
Here sa is a structure array whose field at elements 1 and 2 is in turn another structure array whose third element is a simple scalar structure. The terminal scalar structure has a field which contains a matrix value.
Note that the same result as in the above example could be achieved by:
sa.foo = 1; sa(2).bar(3).baz(1,4) = 5
See also: getfield, rmfield, orderfields, isfield, fieldnames, isstruct, struct.
Package: octave