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
|
<!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>Processing Data in Structures (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Processing Data in Structures (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Processing Data in 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="prev" title="Manipulating 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="Processing-Data-in-Structures">
<div class="nav-panel">
<p>
Previous: <a href="Manipulating-Structures.html" accesskey="p" rel="prev">Manipulating 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="Processing-Data-in-Structures-1"><span>6.1.5 Processing Data in Structures<a class="copiable-link" href="#Processing-Data-in-Structures-1"> ¶</a></span></h4>
<p>The simplest way to process data in a structure is within a <code class="code">for</code>
loop (see <a class="pxref" href="Looping-Over-Structure-Elements.html">Looping Over Structure Elements</a>). A similar effect can be
achieved with the <code class="code">structfun</code> function, where a user defined
function is applied to each field of the structure.
See <a class="xref" href="Function-Application.html#XREFstructfun">structfun</a>.
</p>
<p>Alternatively, to process the data in a structure, the structure might
be converted to another type of container before being treated.
</p>
<a class="anchor" id="XREFstruct2cell"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-struct2cell"><span><code class="def-type"><var class="var">c</var> =</code> <strong class="def-name">struct2cell</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-struct2cell"> ¶</a></span></dt>
<dd><p>Create a new cell array from the objects stored in the struct object.
</p>
<p>If <var class="var">f</var> is the number of fields in the structure, the resulting cell
array will have a dimension vector corresponding to
<code class="code">[<var class="var">f</var> size(<var class="var">s</var>)]</code>. For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">s = struct ("name", {"Peter", "Hannah", "Robert"},
"age", {23, 16, 3});
c = struct2cell (s)
⇒ c = {2x1x3 Cell Array}
c(1,1,:)(:)
⇒
{
[1,1] = Peter
[2,1] = Hannah
[3,1] = Robert
}
c(2,1,:)(:)
⇒
{
[1,1] = 23
[2,1] = 16
[3,1] = 3
}
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="Processing-Data-in-Cell-Arrays.html#XREFcell2struct">cell2struct</a>, <a class="ref" href="#XREFnamedargs2cell">namedargs2cell</a>, <a class="ref" href="Manipulating-Structures.html#XREFfieldnames">fieldnames</a>.
</p></dd></dl>
<a class="anchor" id="XREFnamedargs2cell"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-namedargs2cell"><span><code class="def-type"><var class="var">c</var> =</code> <strong class="def-name">namedargs2cell</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-namedargs2cell"> ¶</a></span></dt>
<dd><p>Create a cell array of field name/value pairs from a scalar structure.
</p>
<p>Example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">s.Name = "Peter";
s.Height = 185;
s.Age = 42;
c = namedargs2cell (s)
⇒ { "Name", "Peter", "Height", 185, "Age", 42 }
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFstruct2cell">struct2cell</a>.
</p></dd></dl>
</div>
</body>
</html>
|