Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 193

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Creating Structures - 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="Structures.html#Structures" title="Structures">
<link rel="prev" href="Structure-Arrays.html#Structure-Arrays" title="Structure Arrays">
<link rel="next" href="Manipulating-Structures.html#Manipulating-Structures" title="Manipulating Structures">
<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="Creating-Structures"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Manipulating-Structures.html#Manipulating-Structures">Manipulating Structures</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Structure-Arrays.html#Structure-Arrays">Structure Arrays</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Structures.html#Structures">Structures</a>
<hr>
</div>

<h4 class="subsection">6.1.3 Creating Structures</h4>

<p><a name="index-dynamic-naming-441"></a>
Besides the index operator ".", Octave can use dynamic naming "(var)" or the
<code>struct</code> function to create structures.  Dynamic naming uses the string
value of a variable as the field name.  For example:

<pre class="example">     a = "field2";
     x.a = 1;
     x.(a) = 2;
     x
          &rArr; x =
             {
               a =  1
               field2 =  2
             }
</pre>
   <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.

<pre class="example">     names = ["Bill"; "Mary"; "John"];
     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>
   <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:

<pre class="example">     struct ("field1", 1, "field2", 2)
     &rArr; ans =
           {
             field1 =  1
             field2 =  2
           }
</pre>
   <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:

<pre class="example">     s = struct ("field1", {1, "one"}, "field2", {2, "two"},
             "field3", 3);
     s.field1
          &rArr;
             ans =  1
             ans = one
     
     s.field2
          &rArr;
             ans =  2
             ans = two
     
     s.field3
          &rArr;
             ans =  3
             ans =  3
</pre>
   <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:

<pre class="example">     struct ("field1", {{1, "one"}}, "field2", 2)
          &rArr; ans =
             {
               field1 =
     
             {
               [1,1] =  1
               [1,2] = one
             }
     
               field2 =  2
             }
</pre>
   <!-- struct src/ov-struct.cc -->
   <p><a name="doc_002dstruct"></a>

<div class="defun">
&mdash; Built-in Function:  <b>struct</b> (<var>"field", value, "field", value, <small class="dots">...</small></var>)<var><a name="index-struct-442"></a></var><br>
<blockquote>
        <p>Create a structure and initialize its value.

        <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>If the argument is an object, return the underlying struct. 
</p></blockquote></div>

   <p>The function <code>isstruct</code> can be used to test if an object is a
structure or a structure array.

<!-- isstruct src/ov-struct.cc -->
   <p><a name="doc_002disstruct"></a>

<div class="defun">
&mdash; Built-in Function:  <b>isstruct</b> (<var>x</var>)<var><a name="index-isstruct-443"></a></var><br>
<blockquote><p>Return true if <var>x</var> is a structure or a structure array. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>, <a href="doc_002discell.html#doc_002discell">iscell</a>, <a href="doc_002disa.html#doc_002disa">isa</a>. 
</p></blockquote></div>

   </body></html>