Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 641ebb3060c35990cc021d8f7aaf9aca > files > 181

octave-doc-5.1.0-7.1.mga7.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Creating Structures (GNU Octave (version 5.1.0))</title>

<meta name="description" content="Creating Structures (GNU Octave (version 5.1.0))">
<meta name="keywords" content="Creating Structures (GNU Octave (version 5.1.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Structures.html#Structures" rel="up" title="Structures">
<link href="Manipulating-Structures.html#Manipulating-Structures" rel="next" title="Manipulating Structures">
<link href="Structure-Arrays.html#Structure-Arrays" rel="prev" title="Structure Arrays">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<a name="Creating-Structures"></a>
<div class="header">
<p>
Next: <a href="Manipulating-Structures.html#Manipulating-Structures" accesskey="n" rel="next">Manipulating Structures</a>, Previous: <a href="Structure-Arrays.html#Structure-Arrays" accesskey="p" rel="prev">Structure Arrays</a>, Up: <a href="Structures.html#Structures" accesskey="u" rel="up">Structures</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Creating-Structures-1"></a>
<h4 class="subsection">6.1.3 Creating Structures</h4>
<a name="index-dynamic-naming"></a>

<p>Besides the index operator <code>&quot;.&quot;</code>, Octave can use dynamic naming
<code>&quot;(var)&quot;</code> or the <code>struct</code> function to create structures.  Dynamic
naming uses the string value of a variable as the field name.  For example:
</p>
<div class="example">
<pre class="example">a = &quot;field2&quot;;
x.a = 1;
x.(a) = 2;
x
     &rArr; x =
        {
          a =  1
          field2 =  2
        }
</pre></div>

<p>Dynamic indexing also allows you to use arbitrary strings, not merely
valid Octave identifiers (note that this does not work on <small>MATLAB</small>):
</p>
<div class="example">
<pre class="example">a = &quot;long field with spaces (and funny char$)&quot;;
x.a = 1;
x.(a) = 2;
x
     &rArr; x =
        {
          a =  1
          long field with spaces (and funny char$) =  2
        }
</pre></div>

<p>The warning id <code>Octave:language-extension</code> can be enabled to warn
about this usage.  See <a href="Issuing-Warnings.html#XREFwarning_005fids">warning_ids</a>.
</p>
<p>More realistically, all of the functions that operate on strings can be used
to build the correct field name before it is entered into the data structure.
</p>
<div class="example">
<pre class="example">names = [&quot;Bill&quot;; &quot;Mary&quot;; &quot;John&quot;];
ages  = [37; 26; 31];
for i = 1:rows (names)
  database.(names(i,:)) = ages(i);
endfor
database
     &rArr; database =
        {
          Bill =  37
          Mary =  26
          John =  31
        }
</pre></div>

<p>The third way to create structures is the <code>struct</code> command.  <code>struct</code>
takes pairs of arguments, where the first argument in the pair is the fieldname
to include in the structure and the second is a scalar or cell array,
representing the values to include in the structure or structure array.  For
example:
</p>
<div class="example">
<pre class="example">struct (&quot;field1&quot;, 1, &quot;field2&quot;, 2)
&rArr; ans =
      {
        field1 =  1
        field2 =  2
      }
</pre></div>

<p>If the values passed to <code>struct</code> are a mix of scalar and cell
arrays, then the scalar arguments are expanded to create a
structure array with a consistent dimension.  For example:
</p>
<div class="example">
<pre class="example">s = struct (&quot;field1&quot;, {1, &quot;one&quot;}, &quot;field2&quot;, {2, &quot;two&quot;},
        &quot;field3&quot;, 3);
s.field1
     &rArr;
        ans =  1
        ans = one

s.field2
     &rArr;
        ans =  2
        ans = two

s.field3
     &rArr;
        ans =  3
        ans =  3
</pre></div>

<p>If you want to create a struct which contains a cell array as an
individual field, you must wrap it in another cell array as shown in
the following example:
</p>
<div class="example">
<pre class="example">struct (&quot;field1&quot;, {{1, &quot;one&quot;}}, &quot;field2&quot;, 2)
     &rArr; ans =
        {
          field1 =

        {
          [1,1] =  1
          [1,2] = one
        }

          field2 =  2
        }
</pre></div>

<a name="XREFstruct"></a><dl>
<dt><a name="index-struct"></a><em><var>s</var> =</em> <strong>struct</strong> <em>()</em></dt>
<dt><a name="index-struct-1"></a><em><var>s</var> =</em> <strong>struct</strong> <em>(<var>field1</var>, <var>value1</var>, <var>field2</var>, <var>value2</var>, &hellip;)</em></dt>
<dt><a name="index-struct-2"></a><em><var>s</var> =</em> <strong>struct</strong> <em>(<var>obj</var>)</em></dt>
<dd>
<p>Create a scalar or array structure and initialize its values.
</p>
<p>The <var>field1</var>, <var>field2</var>, &hellip; variables are strings specifying the
names of the fields and the <var>value1</var>, <var>value2</var>, &hellip; variables
can be of any type.
</p>
<p>If the values are cell arrays, create a structure array and initialize its
values.  The dimensions of each cell array of values must match.  Singleton
cells and non-cell values are repeated so that they fill the entire array.
If the cells are empty, create an empty structure array with the specified
field names.
</p>
<p>If the argument is an object, return the underlying struct.
</p>
<p>Observe that the syntax is optimized for struct <strong>arrays</strong>.  Consider
the following examples:
</p>
<div class="example">
<pre class="example">struct (&quot;foo&quot;, 1)
  &rArr; scalar structure containing the fields:
    foo =  1

struct (&quot;foo&quot;, {})
  &rArr; 0x0 struct array containing the fields:
    foo

struct (&quot;foo&quot;, { {} })
  &rArr; scalar structure containing the fields:
    foo = {}(0x0)

struct (&quot;foo&quot;, {1, 2, 3})
  &rArr; 1x3 struct array containing the fields:
    foo

</pre></div>

<p>The first case is an ordinary scalar struct&mdash;one field, one value.  The
second produces an empty struct array with one field and no values, since
being passed an empty cell array of struct array values.  When the value is
a cell array containing a single entry, this becomes a scalar struct with
that single entry as the value of the field.  That single entry happens
to be an empty cell array.
</p>
<p>Finally, if the value is a non-scalar cell array, then <code>struct</code>
produces a struct <strong>array</strong>.
</p>
<p><strong>See also:</strong> <a href="Processing-Data-in-Cell-Arrays.html#XREFcell2struct">cell2struct</a>, <a href="Manipulating-Structures.html#XREFfieldnames">fieldnames</a>, <a href="Manipulating-Structures.html#XREFgetfield">getfield</a>, <a href="Manipulating-Structures.html#XREFsetfield">setfield</a>, <a href="Manipulating-Structures.html#XREFrmfield">rmfield</a>, <a href="Manipulating-Structures.html#XREFisfield">isfield</a>, <a href="Manipulating-Structures.html#XREForderfields">orderfields</a>, <a href="#XREFisstruct">isstruct</a>, <a href="Function-Application.html#XREFstructfun">structfun</a>.
</p></dd></dl>


<p>The function <code>isstruct</code> can be used to test if an object is a
structure or a structure array.
</p>
<a name="XREFisstruct"></a><dl>
<dt><a name="index-isstruct"></a><em></em> <strong>isstruct</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a structure or a structure array.
</p>
<p><strong>See also:</strong> <a href="Predicates-for-Numeric-Objects.html#XREFismatrix">ismatrix</a>, <a href="Basic-Usage-of-Cell-Arrays.html#XREFiscell">iscell</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<hr>
<div class="header">
<p>
Next: <a href="Manipulating-Structures.html#Manipulating-Structures" accesskey="n" rel="next">Manipulating Structures</a>, Previous: <a href="Structure-Arrays.html#Structure-Arrays" accesskey="p" rel="prev">Structure Arrays</a>, Up: <a href="Structures.html#Structures" accesskey="u" rel="up">Structures</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>