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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Creating Structures (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Creating Structures (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Creating Structures (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Structures.html" rel="up" title="Structures">
<link href="Manipulating-Structures.html" rel="next" title="Manipulating Structures">
<link href="Structure-Arrays.html" rel="prev" title="Structure Arrays">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Creating-Structures">
<div class="nav-panel">
<p>
Next: <a href="Manipulating-Structures.html" accesskey="n" rel="next">Manipulating Structures</a>, Previous: <a href="Structure-Arrays.html" accesskey="p" rel="prev">Structure Arrays</a>, Up: <a href="Structures.html" accesskey="u" rel="up">Structures</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Creating-Structures-1"><span>6.1.3 Creating Structures<a class="copiable-link" href="#Creating-Structures-1"> ¶</a></span></h4>
<a class="index-entry-id" id="index-dynamic-naming"></a>
<p>Besides the index operator <code class="code">"."</code>, Octave can use dynamic naming
<code class="code">"(var)"</code> or the <code class="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">
<div class="group"><pre class="example-preformatted">a = "field2";
x.a = 1;
x.(a) = 2;
x
⇒ x =
{
a = 1
field2 = 2
}
</pre></div></div>
<p>Dynamic indexing also allows you to use arbitrary strings, not merely
valid Octave identifiers (note that this does not work on <small class="sc">MATLAB</small>):
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">a = "long field with spaces (and funny char$)";
x.a = 1;
x.(a) = 2;
x
⇒ x =
{
a = 1
long field with spaces (and funny char$) = 2
}
</pre></div></div>
<p>The warning id <code class="code">Octave:language-extension</code> can be enabled to warn
about this usage. See <a class="xref" 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">
<div class="group"><pre class="example-preformatted">names = ["Bill"; "Mary"; "John"];
ages = [37; 26; 31];
for i = 1:rows (names)
database.(names(i,:)) = ages(i);
endfor
database
⇒ database =
{
Bill = 37
Mary = 26
John = 31
}
</pre></div></div>
<p>The third way to create structures is the <code class="code">struct</code> command. <code class="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">
<div class="group"><pre class="example-preformatted">struct ("field1", 1, "field2", 2)
⇒ ans =
{
field1 = 1
field2 = 2
}
</pre></div></div>
<p>If the values passed to <code class="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">
<div class="group"><pre class="example-preformatted">s = struct ("field1", {1, "one"}, "field2", {2, "two"},
"field3", 3);
s.field1
⇒
ans = 1
ans = one
s.field2
⇒
ans = 2
ans = two
s.field3
⇒
ans = 3
ans = 3
</pre></div></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">
<div class="group"><pre class="example-preformatted">struct ("field1", {{1, "one"}}, "field2", 2)
⇒ ans =
{
field1 =
{
[1,1] = 1
[1,2] = one
}
field2 = 2
}
</pre></div></div>
<a class="anchor" id="XREFstruct"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-struct"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">struct</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-struct"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-struct-1"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">struct</strong> <code class="def-code-arguments">(<var class="var">field1</var>, <var class="var">value1</var>, <var class="var">field2</var>, <var class="var">value2</var>, …)</code><a class="copiable-link" href="#index-struct-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-struct-2"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">struct</strong> <code class="def-code-arguments">(<var class="var">obj</var>)</code><a class="copiable-link" href="#index-struct-2"> ¶</a></span></dt>
<dd>
<p>Create a scalar or array structure and initialize its values.
</p>
<p>The <var class="var">field1</var>, <var class="var">field2</var>, … variables are strings specifying the
names of the fields and the <var class="var">value1</var>, <var class="var">value2</var>, … 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 class="strong">arrays</strong>. Consider
the following examples:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">struct ("foo", 1)
⇒ scalar structure containing the fields:
foo = 1
struct ("foo", {})
⇒ 0x0 struct array containing the fields:
foo
struct ("foo", { {} })
⇒ scalar structure containing the fields:
foo = {}(0x0)
struct ("foo", {1, 2, 3})
⇒ 1x3 struct array containing the fields:
foo
</pre></div></div>
<p>The first case is an ordinary scalar struct—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 nonscalar cell array, then <code class="code">struct</code>
produces a struct <strong class="strong">array</strong>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Processing-Data-in-Cell-Arrays.html#XREFcell2struct">cell2struct</a>, <a class="ref" href="Manipulating-Structures.html#XREFfieldnames">fieldnames</a>, <a class="ref" href="Manipulating-Structures.html#XREFgetfield">getfield</a>, <a class="ref" href="Manipulating-Structures.html#XREFsetfield">setfield</a>, <a class="ref" href="Manipulating-Structures.html#XREFrmfield">rmfield</a>, <a class="ref" href="Manipulating-Structures.html#XREFisfield">isfield</a>, <a class="ref" href="Manipulating-Structures.html#XREForderfields">orderfields</a>, <a class="ref" href="#XREFisstruct">isstruct</a>, <a class="ref" href="Function-Application.html#XREFstructfun">structfun</a>.
</p></dd></dl>
<p>The function <code class="code">isstruct</code> can be used to test if an object is a
structure or a structure array.
</p>
<a class="anchor" id="XREFisstruct"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-isstruct"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">isstruct</strong> <code class="def-code-arguments">(<var class="var">x</var>)</code><a class="copiable-link" href="#index-isstruct"> ¶</a></span></dt>
<dd><p>Return true if <var class="var">x</var> is a structure or a structure array.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFismatrix">ismatrix</a>, <a class="ref" href="Basic-Usage-of-Cell-Arrays.html#XREFiscell">iscell</a>, <a class="ref" href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Manipulating-Structures.html">Manipulating Structures</a>, Previous: <a href="Structure-Arrays.html">Structure Arrays</a>, Up: <a href="Structures.html">Structures</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|