Cell Arrays in Oct-Files (GNU Octave (version 8.1.0))

Next: Structures in Oct-Files, Previous: Character Strings in Oct-Files, Up: Oct-Files [Contents][Index]

#### A.1.4 Cell Arrays in Oct-Files

Octave’s cell type is also available from within oct-files. A cell array is just an `Array` of `octave_value`s, and thus each element of the cell array can be treated like any other `octave_value`. A simple example is

    #include <octave/oct.h>
    #include <octave/Cell.h>

    DEFUN_DLD (celldemo, args, , "Cell Demo")
    {
      if (args.length () != 1)
        print_usage ();

      Cell c = args(0).cell_value ();

      octave_value_list retval;
      retval.resize (c.numel ());    // faster code by pre-declaring size

      for (octave_idx_type i = 0; i < c.numel (); i++)
        {
          retval(i) = c(i);          // using operator syntax
          //retval(i) = c.elem (i);  // using method syntax
        }

      return retval;
    }

Note that cell arrays are used less often in standard oct-files and so the `Cell.h` header file must be explicitly included. The rest of the example extracts the `octave_value`s one by one from the cell array and returns them as individual output arguments. For example:

    [b1, b2, b3] = celldemo ({1, [1, 2], "test"})
    ⇒
    b1 =  1
    b2 =

       1   2

    b3 = test