Next: Short-circuit boolean operators, Previous: Command history, Up: Octave Features
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.
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
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
}
octave:1> x.b.d = 3
x.b.d = 3
octave:2> x.b
ans =
{
d = 3
}
octave:3> x.b.d
ans = 3
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
}
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
}
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.