### 4.3 Data structures

Octave includes a limited amount of support for organizing data in structures. The current implementation uses an associative array with indices limited to strings, but the syntax is more like C-style structures. Here are some examples of using data structures in Octave.

- Elements of structures can be of any value type.

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

                  1  2
                  3  4

                octave:4> x.c
                x.c = string

- Structures may be copied.

                octave:1> y = x
                y =
                {
                  a = 1
                  b =

                    1  2
                    3  4

                  c = string
                  s =

                    0.00000  0.00000  0.00000
                    0.00000  5.46499  0.00000
                    0.00000  0.00000  0.36597

                  u =

                    -0.40455  -0.91451
                    -0.91451   0.40455

                  v =

                    -0.57605   0.81742
                    -0.81742  -0.57605
                }

- Structure elements may reference other structures.

                octave:1> x.b.d = 3
                x.b.d = 3
                octave:2> x.b
                ans =
                {
                  d = 3
                }
                octave:3> x.b.d
                ans = 3

- Functions can return structures.

                octave:1> function y = f (x)
                > y.re = real (x);
                > y.im = imag (x);
                > endfunction

                octave:2> f (rand + rand*I);
                ans =
                {
                  im = 0.18033
                  re = 0.19069
                }

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

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

                    0.00000  0.00000  0.00000
                    0.00000  5.46499  0.00000
                    0.00000  0.00000  0.36597

                  u =

                    -0.40455  -0.91451
                    -0.91451   0.40455

                  v =

                    -0.57605   0.81742
                    -0.81742  -0.57605
                }

- You can also use the function `is_struct` to determine whether a given value is a data structure. For example

                is_struct (x)

  returns 1 if the value of the variable x is a data structure.

This feature should be considered experimental, but you should expect it to work. Suggestions for ways to improve it are welcome.