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
|
<!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>Cell Arrays of Strings (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Cell Arrays of Strings (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Cell Arrays of Strings (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="Processing-Data-in-Cell-Arrays.html" rel="next" title="Processing Data in Cell Arrays">
<link href="Indexing-Cell-Arrays.html" rel="prev" title="Indexing 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="Cell-Arrays-of-Strings">
<div class="nav-panel">
<p>
Next: <a href="Processing-Data-in-Cell-Arrays.html" accesskey="n" rel="next">Processing Data in Cell Arrays</a>, Previous: <a href="Indexing-Cell-Arrays.html" accesskey="p" rel="prev">Indexing 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="Cell-Arrays-of-Strings-1"><span>6.3.4 Cell Arrays of Strings<a class="copiable-link" href="#Cell-Arrays-of-Strings-1"> ¶</a></span></h4>
<p>One common use of cell arrays is to store multiple strings in the same
variable. It is also possible to store multiple strings in a
character matrix by letting each row be a string. This, however,
introduces the problem that all strings must be of equal length.
Therefore, it is recommended to use cell arrays to store multiple
strings. For cases, where the character matrix representation is required
for an operation, there are several functions that convert a cell
array of strings to a character array and back. <code class="code">char</code> and
<code class="code">strvcat</code> convert cell arrays to a character array
(see <a class="pxref" href="Concatenating-Strings.html">Concatenating Strings</a>), while the function <code class="code">cellstr</code>
converts a character array to a cell array of strings:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">a = ["hello"; "world"];
c = cellstr (a)
⇒ c =
{
[1,1] = hello
[2,1] = world
}
</pre></div></div>
<a class="anchor" id="XREFcellstr"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-cellstr"><span><code class="def-type"><var class="var">cstr</var> =</code> <strong class="def-name">cellstr</strong> <code class="def-code-arguments">(<var class="var">strmat</var>)</code><a class="copiable-link" href="#index-cellstr"> ¶</a></span></dt>
<dd><p>Create a new cell array object from the elements of the string array
<var class="var">strmat</var>.
</p>
<p>Each row of <var class="var">strmat</var> becomes an element of <var class="var">cstr</var>. Any trailing
spaces in a row are deleted before conversion.
</p>
<p>To convert back from a cellstr to a character array use <code class="code">char</code>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Creating-Cell-Arrays.html#XREFcell">cell</a>, <a class="ref" href="Concatenating-Strings.html#XREFchar">char</a>.
</p></dd></dl>
<p>One further advantage of using cell arrays to store multiple strings is
that most functions for string manipulations included with Octave
support this representation. As an example, it is possible to compare
one string with many others using the <code class="code">strcmp</code> function. If one of
the arguments to this function is a string and the other is a cell array
of strings, each element of the cell array will be compared to the string
argument:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c = {"hello", "world"};
strcmp ("hello", c)
⇒ ans =
1 0
</pre></div></div>
<p>The following string functions support cell arrays of strings:
<code class="code">char</code>, <code class="code">strvcat</code>, <code class="code">strcat</code> (see <a class="pxref" href="Concatenating-Strings.html">Concatenating Strings</a>), <code class="code">strcmp</code>, <code class="code">strncmp</code>, <code class="code">strcmpi</code>,
<code class="code">strncmpi</code> (see <a class="pxref" href="Searching-in-Strings.html">Searching in Strings</a>), <code class="code">str2double</code>,
<code class="code">deblank</code>, <code class="code">strtrim</code>, <code class="code">strtrunc</code>, <code class="code">strfind</code>,
<code class="code">strmatch</code>, , <code class="code">regexp</code>, <code class="code">regexpi</code>
(see <a class="pxref" href="String-Operations.html">String Operations</a>) and <code class="code">str2double</code>
(see <a class="pxref" href="Converting-Strings.html">Converting Strings</a>).
</p>
<p>The function <code class="code">iscellstr</code> can be used to test if an object is a
cell array of strings.
</p>
<a class="anchor" id="XREFiscellstr"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-iscellstr"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">iscellstr</strong> <code class="def-code-arguments">(<var class="var">cell</var>)</code><a class="copiable-link" href="#index-iscellstr"> ¶</a></span></dt>
<dd><p>Return true if every element of the cell array <var class="var">cell</var> is a character
string.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Character-Arrays.html#XREFischar">ischar</a>, <a class="ref" href="Character-Arrays.html#XREFisstring">isstring</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Processing-Data-in-Cell-Arrays.html">Processing Data in Cell Arrays</a>, Previous: <a href="Indexing-Cell-Arrays.html">Indexing 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>
|