File: Creating-Structures.html

package info (click to toggle)
octave 3.6.2-5%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 71,636 kB
  • sloc: cpp: 241,186; fortran: 23,651; sh: 14,790; ansic: 7,153; lex: 3,761; objc: 3,404; yacc: 3,386; makefile: 2,073; awk: 985; perl: 838
file content (153 lines) | stat: -rw-r--r-- 5,635 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<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>