(octave.info)Manipulating Structures
6.1.4 Manipulating Structures
-----------------------------
Other functions that can manipulate the fields of a structure are given
below.
-- : numfields (S)
Return the number of fields of the structure S.
See also: Note: fieldnames.
-- : NAMES = fieldnames (STRUCT)
-- : NAMES = fieldnames (OBJ)
-- : NAMES = fieldnames (JAVAOBJ)
-- : NAMES = fieldnames ("JAVACLASSNAME")
Return a cell array of strings with the names of the fields in the
specified input.
When the input is a structure STRUCT, the names are the elements of
the structure.
When the input is an Octave object OBJ, the names are the public
properties of the object.
When the input is a Java object JAVAOBJ or a string containing the
name of a Java class JAVACLASSNAME, the names are the public fields
(data members) of the object or class.
See also: Note: numfields, *note isfield:
XREFisfield, Note: orderfields, *note struct:
XREFstruct, Note: methods.
-- : isfield (X, "NAME")
-- : isfield (X, NAME)
Return true if the X is a structure and it includes an element
named NAME.
If NAME is a cell array of strings then a logical array of equal
dimension is returned.
See also: Note: fieldnames.
-- : SOUT = setfield (S, FIELD, VAL)
-- : SOUT = setfield (S, SIDX1, FIELD1, FIDX1, SIDX2, FIELD2, FIDX2,
..., VAL)
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. Note:
warning_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: Note: getfield, Note: rmfield,
Note: orderfields, Note: isfield,
Note: fieldnames, Note: isstruct,
Note: struct.
-- : VAL = getfield (S, FIELD)
-- : VAL = getfield (S, SIDX1, FIELD1, FIDX1, ...)
Get the value of the field named FIELD from a structure or nested
structure S.
If S is a structure array then 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). See ‘setfield’ for a more complete
description of the syntax.
See also: Note: setfield, Note: rmfield,
Note: orderfields, Note: isfield,
Note: fieldnames, Note: isstruct,
Note: struct.
-- : SOUT = rmfield (S, "F")
-- : SOUT = rmfield (S, F)
Return a _copy_ of the structure (array) S with the field F
removed.
If F is a cell array of strings or a character array, remove each
of the named fields.
See also: Note: orderfields, *note fieldnames:
XREFfieldnames, Note: isfield.
-- : SOUT = orderfields (S1)
-- : SOUT = orderfields (S1, S2)
-- : SOUT = orderfields (S1, {CELLSTR})
-- : SOUT = orderfields (S1, P)
-- : [SOUT, P] = orderfields (...)
Return a _copy_ of S1 with fields arranged alphabetically, or as
specified by the second input.
Given one input struct S1, arrange field names alphabetically.
If a second struct argument is given, arrange field names in S1 as
they appear in S2. The second argument may also specify the order
in a cell array of strings CELLSTR. The second argument may also
be a permutation vector.
The optional second output argument P is the permutation vector
which converts the original name order to the new name order.
Examples:
s = struct ("d", 4, "b", 2, "a", 1, "c", 3);
t1 = orderfields (s)
⇒ t1 =
{
a = 1
b = 2
c = 3
d = 4
}
t = struct ("d", {}, "c", {}, "b", {}, "a", {});
t2 = orderfields (s, t)
⇒ t2 =
{
d = 4
c = 3
b = 2
a = 1
}
t3 = orderfields (s, [3, 2, 4, 1])
⇒ t3 =
{
a = 1
b = 2
c = 3
d = 4
}
[t4, p] = orderfields (s, {"d", "c", "b", "a"})
⇒ t4 =
{
d = 4
c = 3
b = 2
a = 1
}
p =
1
4
2
3
See also: Note: fieldnames, *note getfield:
XREFgetfield, Note: setfield, *note rmfield:
XREFrmfield, Note: isfield, *note isstruct:
XREFisstruct, Note: struct.
-- : substruct (TYPE, SUBS, ...)
Create a subscript structure for use with ‘subsref’ or ‘subsasgn’.
For example:
idx = substruct ("()", {3, ":"})
⇒
idx =
{
type = ()
subs =
{
[1,1] = 3
[1,2] = :
}
}
x = [1, 2, 3;
4, 5, 6;
7, 8, 9];
subsref (x, idx)
⇒ 7 8 9
See also: Note: subsref, Note: subsasgn.
automatically generated by info2www version 1.2.2.9