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
|
<!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>Basic Usage of Cell Arrays (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Basic Usage of Cell Arrays (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Basic Usage of Cell Arrays (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="Cell-Arrays.html" rel="up" title="Cell Arrays">
<link href="Creating-Cell-Arrays.html" rel="next" title="Creating Cell 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="Basic-Usage-of-Cell-Arrays">
<div class="nav-panel">
<p>
Next: <a href="Creating-Cell-Arrays.html" accesskey="n" rel="next">Creating Cell Arrays</a>, Up: <a href="Cell-Arrays.html" accesskey="u" rel="up">Cell Arrays</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="Basic-Usage-of-Cell-Arrays-1"><span>6.3.1 Basic Usage of Cell Arrays<a class="copiable-link" href="#Basic-Usage-of-Cell-Arrays-1"> ¶</a></span></h4>
<a class="index-entry-id" id="index-_007b"></a>
<a class="index-entry-id" id="index-_007d"></a>
<p>As an example, the following code creates a cell array containing a
string and a 2-by-2 random matrix
</p>
<div class="example">
<pre class="example-preformatted">c = {"a string", rand(2, 2)};
</pre></div>
<p>To access the elements of a cell array, it can be indexed with the {
and } operators. Thus, the variable created in the previous example
can be indexed like this:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c{1}
⇒ ans = a string
</pre></div></div>
<p>As with numerical arrays several elements of a cell array can be
extracted by indexing with a vector of indexes
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c{1:2}
⇒ ans = a string
⇒ ans =
0.593993 0.627732
0.377037 0.033643
</pre></div></div>
<p>The indexing operators can also be used to insert or overwrite elements
of a cell array. The following code inserts the scalar 3 on the
third place of the previously created cell array
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c{3} = 3
⇒ c =
{
[1,1] = a string
[1,2] =
0.593993 0.627732
0.377037 0.033643
[1,3] = 3
}
</pre></div></div>
<p>Details on indexing cell arrays are explained in <a class="ref" href="Indexing-Cell-Arrays.html">Indexing Cell Arrays</a>.
</p>
<p>In general nested cell arrays are displayed hierarchically as in the
previous example. In some circumstances it makes sense to reference
them by their index, and this can be performed by the <code class="code">celldisp</code>
function.
</p>
<a class="anchor" id="XREFcelldisp"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-celldisp"><span><strong class="def-name">celldisp</strong> <code class="def-code-arguments">(<var class="var">c</var>)</code><a class="copiable-link" href="#index-celldisp"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-celldisp-1"><span><strong class="def-name">celldisp</strong> <code class="def-code-arguments">(<var class="var">c</var>, <var class="var">name</var>)</code><a class="copiable-link" href="#index-celldisp-1"> ¶</a></span></dt>
<dd><p>Recursively display the contents of a cell array.
</p>
<p>By default the values are displayed with the name of the variable <var class="var">c</var>.
However, this name can be replaced with the variable <var class="var">name</var>. For
example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c = {1, 2, {31, 32}};
celldisp (c, "b")
⇒
b{1} =
1
b{2} =
2
b{3}{1} =
31
b{3}{2} =
32
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="Terminal-Output.html#XREFdisp">disp</a>.
</p></dd></dl>
<p>To test if an object is a cell array, use the <code class="code">iscell</code>
function. For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">iscell (c)
⇒ ans = 1
iscell (3)
⇒ ans = 0
</pre></div></div>
<a class="anchor" id="XREFiscell"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-iscell"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">iscell</strong> <code class="def-code-arguments">(<var class="var">x</var>)</code><a class="copiable-link" href="#index-iscell"> ¶</a></span></dt>
<dd><p>Return true if <var class="var">x</var> is a cell array object.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFismatrix">ismatrix</a>, <a class="ref" href="Creating-Structures.html#XREFisstruct">isstruct</a>, <a class="ref" href="Cell-Arrays-of-Strings.html#XREFiscellstr">iscellstr</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="Creating-Cell-Arrays.html">Creating Cell Arrays</a>, Up: <a href="Cell-Arrays.html">Cell Arrays</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>
|