6.1.1 Basic Usage and Examples

Here are some examples of using data structures in Octave.

Elements of structures can be of any value type. For example, the three expressions

x.a = 1;
x.b = [1, 2; 3, 4];
x.c = "string";

create a structure with three elements. The ‘.’ character separates the structure name (in the example above x) from the field name and indicates to Octave that this variable is a structure. To print the value of the structure you can type its name, just as for any other variable:

x
     ⇒ x =

         scalar structure containing the fields:

           a =  1
           b =

              1   2
              3   4

           c = string

Note that Octave may print the elements in any order.

Structures may be copied just like any other variable:

y = x
     ⇒ y =

         scalar structure containing the fields:

           a =  1
           b =

              1   2
              3   4

           c = string

Since structures are themselves values, structure elements may reference other structures, as well. The following statement adds the field d to the structure x. The value of field d is itself a data structure containing the single field a, which has a value of 3.

x.d.a = 3;
x.d
     ⇒ ans =

         scalar structure containing the fields:

           a =  3

x
     ⇒ x =

         scalar structure containing the fields:

           a =  1
           b =

              1   2
              3   4

           c = string
           d =

             scalar structure containing the fields:

               a =  3

Note that when Octave prints the value of a structure that contains other structures, only a few levels are displayed. For example:

a.b.c.d.e = 1;
a
     ⇒ a =

         scalar structure containing the fields:

           b =

             scalar structure containing the fields:

               c =

                 scalar structure containing the fields:

                   d: 1x1 scalar struct

This prevents long and confusing output from large deeply nested structures. The number of levels to print for nested structures may be set with the function struct_levels_to_print, and the function print_struct_array_contents may be used to enable printing of the contents of structure arrays.

val = struct_levels_to_print ()
old_val = struct_levels_to_print (new_val)
struct_levels_to_print (new_val, "local")

Query or set the internal variable that specifies the number of structure levels to display.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

See also: print_struct_array_contents.

val = print_struct_array_contents ()
old_val = print_struct_array_contents (new_val)
print_struct_array_contents (new_val, "local")

Query or set the internal variable that specifies whether to print struct array contents.

If true, values of struct array elements are printed. This variable does not affect scalar structures whose elements are always printed. In both cases, however, printing will be limited to the number of levels specified by struct_levels_to_print.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

See also: struct_levels_to_print.

Functions can return structures. For example, the following function separates the real and complex parts of a matrix and stores them in two elements of the same structure variable y.

function y = f (x)
  y.re = real (x);
  y.im = imag (x);
endfunction

When called with a complex-valued argument, the function f returns the data structure containing the real and imaginary parts of the original function argument.

f (rand (2) + rand (2) * I)
     ⇒ ans =

         scalar structure containing the fields:

           re =

              0.040239  0.242160
              0.238081  0.402523

           im =

              0.26475  0.14828
              0.18436  0.83669

Function return lists can include structure elements, and they may be indexed like any other variable. For example:

[ x.u, x.s(2:3,2:3), x.v ] = svd ([1, 2; 3, 4]);
x

     ⇒ x =

         scalar structure containing the fields:

           u =

             -0.40455  -0.91451
             -0.91451   0.40455

           s =

              0.00000   0.00000   0.00000
              0.00000   5.46499   0.00000
              0.00000   0.00000   0.36597

           v =

             -0.57605   0.81742
             -0.81742  -0.57605

It is also possible to cycle through all the elements of a structure in a loop, using a special form of the for statement (see Looping Over Structure Elements).

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v5.2.0/Basic-Usage-and-Examples.html