1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
<html lang="en">
<head>
<title>Structures in Oct-Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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">
<p>
<a name="Structures-in-Oct-Files"></a>
<a name="Structures-in-Oct_002dFiles"></a>
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 (nargin != 2)
print_usage ();
else
{
Octave_map arg0 = args(0).map_value ();
std::string arg1 = args(1).string_value ();
if (! error_state && arg0.contains (arg1))
{
// The following two lines might be written as
// octave_value tmp;
// for (Octave_map::iterator p0 =
// arg0.begin();
// p0 != arg0.end(); p0++ )
// if (arg0.key (p0) == arg1)
// {
// tmp = arg0.contents (p0) (0);
// break;
// }
// though using seek is more concise.
Octave_map::const_iterator p1 = arg0.seek (arg1);
octave_value tmp = arg0.contents(p1)(0);
Octave_map st;
st.assign ("selected", tmp);
retval = octave_value (st);
}
}
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>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>
|