- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

6.1.2 Creating Structures

As well as indexing a structure with ".", Octave can create a structure with the struct command. struct takes pairs of arguments, where the first argument in the pair is the fieldname to include in the structure and the second is a scalar or cell array, representing the values to include in the structure or structure array. For example

struct ("field1", 1, "field2", 2)
=> ans =
        field1 =  1
        field2 =  2

If the values passed to struct are a mix of scalar and cell arrays, then the scalar arguments are expanded to create a structure array with a consistent dimension. For example

struct ("field1", {1, "one"}, "field2", {2, "two"},
        "field3", 3)
=> ans =
        field1 =
          [1] =  1
          [2] = one
        field2 =
          [1] =  2
          [2] = two
        field3 =
          [1] =  3
          [2] =  3

Built-in Function: struct ("field", value, "field", value, ...)

Create a structure and initialize its value.

If the values are cell arrays, create a structure array and initialize its values. The dimensions of each cell array of values must match. Singleton cells and non-cell values are repeated so that they fill the entire array. If the cells are empty, create an empty structure array with the specified field names.

Built-in Function: isstruct (expr)
Return 1 if the value of the expression expr is a structure.

Additional functions that can manipulate the fields of a structure are listed below.

Built-in Function: rmfield (s, f)
Remove field f from the structure s. If f is a cell array of character strings or a character array, remove the named fields.

See also cellstr, iscellstr, setfield

Function File: [k1, ..., v1] = setfield (s, k1, v1, ...)
Set field members in a structure.

oo(1,1).f0= 1;
oo = setfield(oo,{1,2},'fd',{3},'b', 6);
oo(1,2).fd(3).b == 6
=> ans = 1

Note that this function could be written

i1= {1,2}; i2= 'fd'; i3= {3}; i4= 'b';
oo( i1{:} ).( i2 )( i3{:} ).( i4 ) == 6;

See also getfield, rmfield, isfield, isstruct, fieldnames, struct

Function File: [t, p] = orderfields (s1, s2)
Return a struct with fields arranged alphabetically or as specified by s2 and a corresponding permutation vector.

Given one struct, arrange field names in s1 alphabetically.

Given two structs, arrange field names in s1 as they appear in s2. The second argument may also specify the order in a permutation vector or a cell array of strings.

See also getfield, rmfield, isfield, isstruct, fieldnames, struct

ISBN 095461206XGNU Octave Manual Version 3See the print edition