<html lang="en"> <head> <title>Structures in Oct-Files - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files"> <link rel="prev" href="Cell-Arrays-in-Oct_002dFiles.html#Cell-Arrays-in-Oct_002dFiles" title="Cell Arrays in Oct-Files"> <link rel="next" href="Sparse-Matrices-in-Oct_002dFiles.html#Sparse-Matrices-in-Oct_002dFiles" title="Sparse Matrices in Oct-Files"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Structures-in-Oct-Files"></a> <a name="Structures-in-Oct_002dFiles"></a> <p> Next: <a rel="next" accesskey="n" href="Sparse-Matrices-in-Oct_002dFiles.html#Sparse-Matrices-in-Oct_002dFiles">Sparse Matrices in Oct-Files</a>, Previous: <a rel="previous" accesskey="p" href="Cell-Arrays-in-Oct_002dFiles.html#Cell-Arrays-in-Oct_002dFiles">Cell Arrays in Oct-Files</a>, Up: <a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a> <hr> </div> <h4 class="subsection">A.1.5 Structures in Oct-Files</h4> <p>A structure in Octave is map between a number of fields represented and their values. The Standard Template Library <code>map</code> class is used, with the pair consisting of a <code>std::string</code> and an octave <code>Cell</code> variable. <p>A simple example demonstrating the use of structures within oct-files is <pre class="example"><pre class="verbatim"> #include <octave/oct.h> #include <octave/ov-struct.h> DEFUN_DLD (structdemo, args, , "Struct demo.") { int nargin = args.length (); octave_value retval; if (args.length () == 2) { octave_scalar_map arg0 = args(0).scalar_map_value (); if (! error_state) { std::string arg1 = args(1).string_value (); if (! error_state) { octave_value tmp = arg0.contents (arg1); if (tmp.is_defined ()) { octave_scalar_map st; st.assign ("selected", tmp); retval = octave_value (st); } else error ("sruct does not contain field named '%s'\n", arg1.c_str ()); } else error ("expecting character string as second argument"); } else error ("expecting struct as first argument"); } else print_usage (); return retval; } </pre> </pre> <p>An example of its use is <pre class="example"> x.a = 1; x.b = "test"; x.c = [1, 2]; structdemo (x, "b") ⇒ selected = test </pre> <p>The commented code above demonstrates how to iterate over all of the fields of the structure, where as the following code demonstrates finding a particular field in a more concise manner. <p>As can be seen the <code>contents</code> method of the <code>Octave_map</code> class returns a <code>Cell</code> which allows structure arrays to be represented. Therefore, to obtain the underlying <code>octave_value</code> we write <pre class="example"> octave_value tmp = arg0.contents (p1) (0); </pre> <p class="noindent">where the trailing (0) is the () operator on the <code>Cell</code> object. We can equally iterate of the elements of the Cell array to address the elements of the structure array. </body></html>