Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 117

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

<html lang="en">
<head>
<title>Basic Usage and Examples - 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="next" href="Structure-Arrays.html#Structure-Arrays" title="Structure Arrays">
<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="Basic-Usage-and-Examples"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" 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.1 Basic Usage and Examples</h4>

<p>Here are some examples of using data structures in Octave.

   <p>Elements of structures can be of any value type.  For example, the three
expressions

<pre class="example">     x.a = 1;
     x.b = [1, 2; 3, 4];
     x.c = "string";
</pre>
   <p><a name="index-g_t_002e-434"></a>create a structure with three elements.  The &lsquo;<samp><span class="samp">.</span></samp>&rsquo; character separates
the structure name from the field name and indicates to Octave that this
variable is a structure.  To print the value of the
structure you can type its name, just as for any other variable:

<pre class="example">     x
          &rArr; x =
             {
               a = 1
               b =
     
                 1  2
                 3  4
     
               c = string
             }
</pre>
   <p class="noindent">Note that Octave may print the elements in any order.

   <p>Structures may be copied just like any other variable:

<pre class="example">     y = x
          &rArr; y =
             {
               a = 1
               b =
     
                 1  2
                 3  4
     
               c = string
             }
</pre>
   <p>Since structures are themselves values, structure elements may reference
other structures.  The following statements change the value of the
element <code>b</code> of the structure <code>x</code> to be a data structure
containing the single element <code>d</code>, which has a value of 3.

<pre class="example">     x.b.d = 3;
     x.b
          &rArr; ans =
             {
               d = 3
             }
     
     x
          &rArr; x =
             {
               a = 1
               b =
               {
                 d = 3
               }
     
               c = string
             }
</pre>
   <p>Note that when Octave prints the value of a structure that contains
other structures, only a few levels are displayed.  For example:

<pre class="example">     a.b.c.d.e = 1;
     a
          &rArr; a =
             {
               b =
               {
                 c =
                 {
                   1x1 struct array containing the fields:
     
                   d: 1x1 struct
                 }
               }
             }
</pre>
   <p class="noindent">This prevents long and confusing output from large deeply nested
structures.  The number of levels to print for nested structures may be
set with the function <code>struct_levels_to_print</code>, and the function
<code>print_struct_array_contents</code> may be used to enable printing of the
contents of structure arrays.

<!-- struct_levels_to_print src/ov-struct.cc -->
   <p><a name="doc_002dstruct_005flevels_005fto_005fprint"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>struct_levels_to_print</b> ()<var><a name="index-struct_005flevels_005fto_005fprint-435"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>struct_levels_to_print</b> (<var>new_val</var>)<var><a name="index-struct_005flevels_005fto_005fprint-436"></a></var><br>
&mdash; Built-in Function:  <b>struct_levels_to_print</b> (<var>new_val, "local"</var>)<var><a name="index-struct_005flevels_005fto_005fprint-437"></a></var><br>
<blockquote><p>Query or set the internal variable that specifies the number of
structure levels to display.

        <p>When called from inside a function with the "local" option, the variable is
changed locally for the function and any subroutines it calls.  The original
variable value is restored when exiting the function. 
</p></blockquote></div>

<!-- print_struct_array_contents src/ov-struct.cc -->
   <p><a name="doc_002dprint_005fstruct_005farray_005fcontents"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>print_struct_array_contents</b> ()<var><a name="index-print_005fstruct_005farray_005fcontents-438"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>print_struct_array_contents</b> (<var>new_val</var>)<var><a name="index-print_005fstruct_005farray_005fcontents-439"></a></var><br>
&mdash; Built-in Function:  <b>print_struct_array_contents</b> (<var>new_val, "local"</var>)<var><a name="index-print_005fstruct_005farray_005fcontents-440"></a></var><br>
<blockquote><p>Query or set the internal variable that specifies whether to print struct
array contents.  If true, values of struct array elements are printed. 
This variable does not affect scalar structures.  Their elements
are always printed.  In both cases, however, printing will be limited to
the number of levels specified by <var>struct_levels_to_print</var>.

        <p>When called from inside a function with the "local" option, the variable is
changed locally for the function and any subroutines it calls.  The original
variable value is restored when exiting the function. 
</p></blockquote></div>

   <p>Functions can return structures.  For example, the following function
separates the real and complex parts of a matrix and stores them in two
elements of the same structure variable.

<pre class="example">     function y = f (x)
       y.re = real (x);
       y.im = imag (x);
     endfunction
</pre>
   <p>When called with a complex-valued argument, <code>f</code> returns the data
structure containing the real and imaginary parts of the original
function argument.

<pre class="example">     f (rand (2) + rand (2) * I)
          &rArr; ans =
             {
               im =
     
                 0.26475  0.14828
                 0.18436  0.83669
     
               re =
     
                 0.040239  0.242160
                 0.238081  0.402523
     
             }
</pre>
   <p>Function return lists can include structure elements, and they may be
indexed like any other variable.  For example:

<pre class="example">     [ x.u, x.s(2:3,2:3), x.v ] = svd ([1, 2; 3, 4]);
     x
          &rArr; x =
             {
               u =
     
                 -0.40455  -0.91451
                 -0.91451   0.40455
     
               s =
     
                  0.00000   0.00000   0.00000
                  0.00000   5.46499   0.00000
                  0.00000   0.00000   0.36597
     
               v =
     
                 -0.57605   0.81742
                 -0.81742  -0.57605
     
             }
</pre>
   <p>It is also possible to cycle through all the elements of a structure in
a loop, using a special form of the <code>for</code> statement
(see <a href="Looping-Over-Structure-Elements.html#Looping-Over-Structure-Elements">Looping Over Structure Elements</a>).

   </body></html>