Frequently asked questions about Octave (with answers): Octave Features

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 3. What features are unique to Octave?

3.1 Command and variable name completion |  |
3.2 Command history |  |
3.3 Data structures |  |
3.4 Short-circuit boolean operators |  |
3.5 Increment and decrement operators |  |
3.6 Unwind-protect |  |
3.7 Variable-length argument lists |  |
3.8 Variable-length return lists |  |
3.9 Built-in ODE and DAE solvers |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.1 Command and variable name completion

Typing a TAB character (ASCII code 9) on the command line causes Octave to attempt to complete variable, function, and file names. Octave uses the text before the cursor as the initial portion of the name to complete.

For example, if you type ``fu`' followed by TAB at the Octave prompt, Octave will complete the rest of the name ``function`' on the command line (unless you have other variables or functions defined that begin with the characters ``fu`'). If there is more than one possible completion, Octave will ring the terminal bell to let you know that your initial sequence of characters is not enough to specify a unique name. To complete the name, you may either edit the initial character sequence (usually adding more characters until completion is possible) or type another TAB to cause Octave to display the list of possible completions.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.2 Command history

When running interactively, Octave saves the commands you type in an internal buffer so that you can recall and edit them. Emacs and vi editing modes are available with Emacs keybindings enabled by default.

When Octave exits, the current command history is saved to the file ``~/.octave_hist`', and each time Octave starts, it inserts the contents of the ``~/.octave_hist`' file in the history list so that it is easy to begin working where you left off.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.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.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.4 Short-circuit boolean operators

Octave's ``&&`' and ``||`' logical operators are evaluated in a short-circuit fashion (like the corresponding operators in the C language) and work differently than the element by element operators ``&`' and ``|`'.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.5 Increment and decrement operators

Octave includes the C-like increment and decrement operators ``++`' and ``--`' in both their prefix and postfix forms.

For example, to pre-increment the variable x, you would write `++x`. This would add one to x and then return the new value of x as the result of the expression. It is exactly the same as the expression `x = x + 1`.

To post-increment a variable x, you would write `x++`. This adds one to the variable x, but returns the value that x had prior to incrementing it. For example, if x is equal to 2, the result of the expression `x++` is 2, and the new value of x is 3.

For matrix and vector arguments, the increment and decrement operators work on each element of the operand.

It is not currently possible to increment index expressions. For example, you might expect that the expression `v(4)++` would increment the fourth element of the vector v, but instead it results in a parse error. This problem may be fixed in a future release of Octave.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.6 Unwind-protect

Octave supports a limited form of exception handling modelled after the unwind-protect form of Lisp. The general form of an `unwind_protect` block looks like this:

    unwind_protect
      body
    unwind_protect_cleanup
      cleanup
    end_unwind_protect

Where body and cleanup are both optional and may contain any Octave expressions or commands. The statements in cleanup are guaranteed to be executed regardless of how control exits body.

The `unwind_protect` statement is often used to reliably restore the values of global variables that need to be temporarily changed.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.7 Variable-length argument lists

Octave has a real mechanism for handling functions that take an unspecified number of arguments, so it is no longer necessary to place an upper bound on the number of optional arguments that a function can accept.

Here is an example of a function that uses the new syntax to print a header followed by an unspecified number of values:

    function foo (heading, ...)
      disp (heading);
      va_start ();
      while (--nargin)
        disp (va_arg ());
      endwhile
    endfunction

Calling `va_start()` positions an internal pointer to the first unnamed argument and allows you to cycle through the arguments more than once. It is not necessary to call `va_start()` if you do not plan to cycle through the arguments more than once.

The function `va_arg()` returns the value of the next available argument and moves the internal pointer to the next argument. It is an error to call `va_arg()` when there are no more arguments available.

It is also possible to use the keyword all_va_args to pass all unnamed arguments to another function.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.8 Variable-length return lists

Octave also has a real mechanism for handling functions that return an unspecified number of values, so it is no longer necessary to place an upper bound on the number of outputs that a function can produce.

Here is an example of a function that uses the new syntax to produce ``N`' values:

    function [...] = foo (n)
      for i = 1:n
        vr_val (i);
      endfor
    endfunction

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 3.9 Built-in ODE and DAE solvers

Octave includes LSODE and DASSL for solving systems of stiff ordinary differential and differential-algebraic equations. These functions are built in to the interpreter.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by John W. Eaton on January, 20 2004 using texi2html