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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
<!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>Manipulating Structures (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Manipulating Structures (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Manipulating 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="Processing-Data-in-Structures.html" rel="next" title="Processing Data in Structures">
<link href="Creating-Structures.html" rel="prev" title="Creating Structures">
<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="Manipulating-Structures">
<div class="nav-panel">
<p>
Next: <a href="Processing-Data-in-Structures.html" accesskey="n" rel="next">Processing Data in Structures</a>, Previous: <a href="Creating-Structures.html" accesskey="p" rel="prev">Creating Structures</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="Manipulating-Structures-1"><span>6.1.4 Manipulating Structures<a class="copiable-link" href="#Manipulating-Structures-1"> ¶</a></span></h4>
<p>Other functions that can manipulate the fields of a structure are given below.
</p>
<a class="anchor" id="XREFnumfields"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-numfields"><span><code class="def-type"><var class="var">n</var> =</code> <strong class="def-name">numfields</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-numfields"> ¶</a></span></dt>
<dd><p>Return the number of fields of the structure <var class="var">s</var>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFfieldnames">fieldnames</a>.
</p></dd></dl>
<a class="anchor" id="XREFfieldnames"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-fieldnames"><span><code class="def-type"><var class="var">names</var> =</code> <strong class="def-name">fieldnames</strong> <code class="def-code-arguments">(<var class="var">struct</var>)</code><a class="copiable-link" href="#index-fieldnames"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fieldnames-1"><span><code class="def-type"><var class="var">names</var> =</code> <strong class="def-name">fieldnames</strong> <code class="def-code-arguments">(<var class="var">obj</var>)</code><a class="copiable-link" href="#index-fieldnames-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fieldnames-2"><span><code class="def-type"><var class="var">names</var> =</code> <strong class="def-name">fieldnames</strong> <code class="def-code-arguments">(<var class="var">javaobj</var>)</code><a class="copiable-link" href="#index-fieldnames-2"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-fieldnames-3"><span><code class="def-type"><var class="var">names</var> =</code> <strong class="def-name">fieldnames</strong> <code class="def-code-arguments">("<var class="var">javaclassname</var>")</code><a class="copiable-link" href="#index-fieldnames-3"> ¶</a></span></dt>
<dd><p>Return a cell array of strings with the names of the fields in the specified
input.
</p>
<p>When the input is a structure <var class="var">struct</var>, the <var class="var">names</var> are the elements
of the structure.
</p>
<p>When the input is an Octave object <var class="var">obj</var>, the <var class="var">names</var> are the public
properties of the object.
</p>
<p>When the input is a Java object <var class="var">javaobj</var> or a string containing the
name of a Java class <var class="var">javaclassname</var>, the <var class="var">names</var> are the public
fields (data members) of the object or class.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFnumfields">numfields</a>, <a class="ref" href="#XREFisfield">isfield</a>, <a class="ref" href="#XREForderfields">orderfields</a>, <a class="ref" href="Creating-Structures.html#XREFstruct">struct</a>, <a class="ref" href="Properties.html#XREFproperties">properties</a>.
</p></dd></dl>
<a class="anchor" id="XREFisfield"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-isfield"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">isfield</strong> <code class="def-code-arguments">(<var class="var">x</var>, "<var class="var">name</var>")</code><a class="copiable-link" href="#index-isfield"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-isfield-1"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">isfield</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">name</var>)</code><a class="copiable-link" href="#index-isfield-1"> ¶</a></span></dt>
<dd><p>Return true if the <var class="var">x</var> is a structure and it includes an element named
<var class="var">name</var>.
</p>
<p>If <var class="var">name</var> is a cell array of strings then a logical array of equal
dimension is returned.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFfieldnames">fieldnames</a>.
</p></dd></dl>
<a class="anchor" id="XREFsetfield"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-setfield"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">setfield</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">field</var>, <var class="var">val</var>)</code><a class="copiable-link" href="#index-setfield"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-setfield-1"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">setfield</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">sidx1</var>, <var class="var">field1</var>, <var class="var">fidx1</var>, <var class="var">sidx2</var>, <var class="var">field2</var>, <var class="var">fidx2</var>, …, <var class="var">val</var>)</code><a class="copiable-link" href="#index-setfield-1"> ¶</a></span></dt>
<dd>
<p>Return a <em class="emph">copy</em> of the structure <var class="var">s</var> with the field member
<var class="var">field</var> set to the value <var class="var">val</var>.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"><var class="var">s</var> = struct ();
<var class="var">s</var> = setfield (<var class="var">s</var>, "foo bar", 42);
</pre></div></div>
<p>This is equivalent to
</p>
<div class="example">
<pre class="example-preformatted"><var class="var">s</var>.("foo bar") = 42;
</pre></div>
<p>Note that ordinary structure syntax <code class="code"><var class="var">s</var>.foo bar = 42</code> cannot be
used here, as the field name is not a valid Octave identifier because of
the space character. Using arbitrary strings for field names is
incompatible with <small class="sc">MATLAB</small>, and this usage will emit a warning if the
warning ID <code class="code">Octave:language-extension</code> is enabled.
See <a class="xref" href="Issuing-Warnings.html#XREFwarning_005fids"><code class="code">warning_ids</code></a>.
</p>
<p>With the second calling form, set a field of a structure array. The
input <var class="var">sidx</var> selects an element of the structure array, <var class="var">field</var>
specifies the field name of the selected element, and <var class="var">fidx</var> selects
which element of the field (in the case of an array or cell array).
The <var class="var">sidx</var>, <var class="var">field</var>, and <var class="var">fidx</var> inputs can be repeated to
address nested structure array elements. The structure array index and
field element index must be cell arrays while the field name must be a
string.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"><var class="var">s</var> = struct ("baz", 42);
setfield (<var class="var">s</var>, {1}, "foo", {1}, "bar", 54)
⇒
ans =
scalar structure containing the fields:
baz = 42
foo =
scalar structure containing the fields:
bar = 54
</pre></div></div>
<p>The example begins with an ordinary scalar structure to which a nested
scalar structure is added. In all cases, if the structure index <var class="var">sidx</var>
is not specified it defaults to 1 (scalar structure). Thus, the example
above could be written more concisely as
<code class="code">setfield (<var class="var">s</var>, "foo", "bar", 54)</code>
</p>
<p>Finally, an example with nested structure arrays:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"><var class="var">sa</var>.foo = 1;
<var class="var">sa</var> = setfield (<var class="var">sa</var>, {2}, "bar", {3}, "baz", {1, 4}, 5);
<var class="var">sa</var>(2).bar(3)
⇒
ans =
scalar structure containing the fields:
baz = 0 0 0 5
</pre></div></div>
<p>Here <var class="var">sa</var> is a structure array whose field at elements 1 and 2 is in
turn another structure array whose third element is a simple scalar
structure. The terminal scalar structure has a field which contains a
matrix value.
</p>
<p>Note that the same result as in the above example could be achieved by:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"><var class="var">sa</var>.foo = 1;
<var class="var">sa</var>(2).bar(3).baz(1,4) = 5
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFgetfield">getfield</a>, <a class="ref" href="#XREFrmfield">rmfield</a>, <a class="ref" href="#XREForderfields">orderfields</a>, <a class="ref" href="#XREFisfield">isfield</a>, <a class="ref" href="#XREFfieldnames">fieldnames</a>, <a class="ref" href="Creating-Structures.html#XREFisstruct">isstruct</a>, <a class="ref" href="Creating-Structures.html#XREFstruct">struct</a>.
</p></dd></dl>
<a class="anchor" id="XREFgetfield"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-getfield"><span><code class="def-type"><var class="var">val</var> =</code> <strong class="def-name">getfield</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">field</var>)</code><a class="copiable-link" href="#index-getfield"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-getfield-1"><span><code class="def-type"><var class="var">val</var> =</code> <strong class="def-name">getfield</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">sidx1</var>, <var class="var">field1</var>, <var class="var">fidx1</var>, …)</code><a class="copiable-link" href="#index-getfield-1"> ¶</a></span></dt>
<dd><p>Get the value of the field named <var class="var">field</var> from a structure or nested
structure <var class="var">s</var>.
</p>
<p>If <var class="var">s</var> is a structure array then <var class="var">sidx</var> selects an element of the
structure array, <var class="var">field</var> specifies the field name of the selected
element, and <var class="var">fidx</var> selects which element of the field (in the case of
an array or cell array). For a more complete description of the syntax,
see <a class="pxref" href="#XREFsetfield"><code class="code">setfield</code></a>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFsetfield">setfield</a>, <a class="ref" href="#XREFrmfield">rmfield</a>, <a class="ref" href="#XREForderfields">orderfields</a>, <a class="ref" href="#XREFisfield">isfield</a>, <a class="ref" href="#XREFfieldnames">fieldnames</a>, <a class="ref" href="Creating-Structures.html#XREFisstruct">isstruct</a>, <a class="ref" href="Creating-Structures.html#XREFstruct">struct</a>.
</p></dd></dl>
<a class="anchor" id="XREFrmfield"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-rmfield"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">rmfield</strong> <code class="def-code-arguments">(<var class="var">s</var>, "<var class="var">f</var>")</code><a class="copiable-link" href="#index-rmfield"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-rmfield-1"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">rmfield</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">f</var>)</code><a class="copiable-link" href="#index-rmfield-1"> ¶</a></span></dt>
<dd><p>Return a <em class="emph">copy</em> of the structure (array) <var class="var">s</var> with the field <var class="var">f</var>
removed.
</p>
<p>If <var class="var">f</var> is a cell array of strings or a character array, remove each of
the named fields.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREForderfields">orderfields</a>, <a class="ref" href="#XREFfieldnames">fieldnames</a>, <a class="ref" href="#XREFisfield">isfield</a>.
</p></dd></dl>
<a class="anchor" id="XREForderfields"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-orderfields"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">orderfields</strong> <code class="def-code-arguments">(<var class="var">s1</var>)</code><a class="copiable-link" href="#index-orderfields"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-orderfields-1"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">orderfields</strong> <code class="def-code-arguments">(<var class="var">s1</var>, <var class="var">s2</var>)</code><a class="copiable-link" href="#index-orderfields-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-orderfields-2"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">orderfields</strong> <code class="def-code-arguments">(<var class="var">s1</var>, {<var class="var">cellstr</var>})</code><a class="copiable-link" href="#index-orderfields-2"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-orderfields-3"><span><code class="def-type"><var class="var">sout</var> =</code> <strong class="def-name">orderfields</strong> <code class="def-code-arguments">(<var class="var">s1</var>, <var class="var">p</var>)</code><a class="copiable-link" href="#index-orderfields-3"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-orderfields-4"><span><code class="def-type">[<var class="var">sout</var>, <var class="var">p</var>] =</code> <strong class="def-name">orderfields</strong> <code class="def-code-arguments">(…)</code><a class="copiable-link" href="#index-orderfields-4"> ¶</a></span></dt>
<dd><p>Return a <em class="emph">copy</em> of <var class="var">s1</var> with fields arranged alphabetically, or as
specified by the second input.
</p>
<p>Given one input struct <var class="var">s1</var>, arrange field names alphabetically.
</p>
<p>If a second struct argument is given, arrange field names in <var class="var">s1</var> as
they appear in <var class="var">s2</var>. The second argument may also specify the order
in a cell array of strings <var class="var">cellstr</var>. The second argument may also
be a permutation vector.
</p>
<p>The optional second output argument <var class="var">p</var> is the permutation vector which
converts the original name order to the new name order.
</p>
<p>Examples:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">s = struct ("d", 4, "b", 2, "a", 1, "c", 3);
t1 = orderfields (s)
⇒ t1 =
scalar structure containing the fields:
a = 1
b = 2
c = 3
d = 4
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">t = struct ("d", {}, "c", {}, "b", {}, "a", {});
t2 = orderfields (s, t)
⇒ t2 =
scalar structure containing the fields:
d = 4
c = 3
b = 2
a = 1
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">t3 = orderfields (s, [3, 2, 4, 1])
⇒ t3 =
scalar structure containing the fields:
a = 1
b = 2
c = 3
d = 4
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">[t4, p] = orderfields (s, {"d", "c", "b", "a"})
⇒ t4 =
scalar structure containing the fields:
d = 4
c = 3
b = 2
a = 1
p =
1
4
2
3
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFfieldnames">fieldnames</a>, <a class="ref" href="#XREFgetfield">getfield</a>, <a class="ref" href="#XREFsetfield">setfield</a>, <a class="ref" href="#XREFrmfield">rmfield</a>, <a class="ref" href="#XREFisfield">isfield</a>, <a class="ref" href="Creating-Structures.html#XREFisstruct">isstruct</a>, <a class="ref" href="Creating-Structures.html#XREFstruct">struct</a>.
</p></dd></dl>
<a class="anchor" id="XREFsubstruct"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-substruct"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">substruct</strong> <code class="def-code-arguments">(<var class="var">type</var>, <var class="var">subs</var>, …)</code><a class="copiable-link" href="#index-substruct"> ¶</a></span></dt>
<dd><p>Create a subscript structure for use with <code class="code">subsref</code> or <code class="code">subsasgn</code>.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">idx = substruct ("()", {3, ":"})
⇒ idx =
scalar structure containing the fields:
type = ()
subs =
{
[1,1] = 3
[1,2] = :
}
x = [1, 2, 3;
4, 5, 6;
7, 8, 9];
subsref (x, idx)
⇒ 7 8 9
</pre></div></div>
<p>Note: The keyword <code class="code">end</code> cannot be used within <code class="code">subsref</code> or
<code class="code">subsasgn</code> for indexing assignments.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Defining-Indexing-And-Indexed-Assignment.html#XREFsubsref">subsref</a>, <a class="ref" href="Defining-Indexing-And-Indexed-Assignment.html#XREFsubsasgn">subsasgn</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Processing-Data-in-Structures.html">Processing Data in Structures</a>, Previous: <a href="Creating-Structures.html">Creating Structures</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>
|