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
|
<!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>Character Arrays (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Character Arrays (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Character 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="Strings.html" rel="up" title="Strings">
<link href="String-Operations.html" rel="next" title="String Operations">
<link href="Escape-Sequences-in-String-Constants.html" rel="prev" title="Escape Sequences in String Constants">
<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="section-level-extent" id="Character-Arrays">
<div class="nav-panel">
<p>
Next: <a href="String-Operations.html" accesskey="n" rel="next">String Operations</a>, Previous: <a href="Escape-Sequences-in-String-Constants.html" accesskey="p" rel="prev">Escape Sequences in String Constants</a>, Up: <a href="Strings.html" accesskey="u" rel="up">Strings</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>
<h3 class="section" id="Character-Arrays-1"><span>5.2 Character Arrays<a class="copiable-link" href="#Character-Arrays-1"> ¶</a></span></h3>
<p>The string representation used by Octave is an array of characters, so
internally the string <code class="code">"dddddddddd"</code> is actually a row vector
of length 10 containing the value 100 in all places (100 is the ASCII code of
<code class="code">"d"</code>). This lends itself to the obvious generalization to character
matrices. Using a matrix of characters, it is possible to represent a
collection of same-length strings in one variable. The convention used in
Octave is that each row in a character matrix is a separate string, but letting
each column represent a string is equally possible.
</p>
<p>The easiest way to create a character matrix is to put several strings
together into a matrix.
</p>
<div class="example">
<pre class="example-preformatted">collection = [ "String #1"; "String #2" ];
</pre></div>
<p>This creates a 2-by-9 character matrix.
</p>
<p>The function <code class="code">ischar</code> can be used to test if an object is a character
matrix.
</p>
<a class="anchor" id="XREFischar"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-ischar"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">ischar</strong> <code class="def-code-arguments">(<var class="var">x</var>)</code><a class="copiable-link" href="#index-ischar"> ¶</a></span></dt>
<dd><p>Return true if <var class="var">x</var> is a character array.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFisfloat">isfloat</a>, <a class="ref" href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFislogical">islogical</a>, <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFisnumeric">isnumeric</a>, <a class="ref" href="#XREFisstring">isstring</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>
<a class="anchor" id="XREFisstring"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-isstring"><span><code class="def-type"><var class="var">tf</var> =</code> <strong class="def-name">isstring</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-isstring"> ¶</a></span></dt>
<dd><p>Return true if <var class="var">s</var> is a string array.
</p>
<p>A string array is a data type that stores strings (row vectors of
characters) at each element in the array. It is distinct from character
arrays which are N-dimensional arrays where each element is a single 1x1
character. It is also distinct from cell arrays of strings which store
strings at each element, but use cell indexing ‘<samp class="samp">{}</samp>’ to access
elements rather than string arrays which use ordinary array indexing
‘<samp class="samp">()</samp>’.
</p>
<p>Programming Note: Octave does not yet implement string arrays so this
function will always return false.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFischar">ischar</a>, <a class="ref" href="Cell-Arrays-of-Strings.html#XREFiscellstr">iscellstr</a>, <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFisfloat">isfloat</a>, <a class="ref" href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFislogical">islogical</a>, <a class="ref" href="Predicates-for-Numeric-Objects.html#XREFisnumeric">isnumeric</a>, <a class="ref" href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>
<p>To test if an object is a string (i.e., a 1xN row vector of
characters and not a character matrix) you can use the <code class="code">ischar</code> function
in combination with the <code class="code">isrow</code> function as in the following example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">ischar (collection)
⇒ 1
ischar (collection) && isrow (collection)
⇒ 0
ischar ("my string") && isrow ("my string")
⇒ 1
</pre></div></div>
<p>One relevant question is, what happens when a character matrix is
created from strings of different length. The answer is that Octave
puts blank characters at the end of strings shorter than the longest
string. It is possible to use a different character than the
blank character using the <code class="code">string_fill_char</code> function.
</p>
<a class="anchor" id="XREFstring_005ffill_005fchar"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-string_005ffill_005fchar"><span><code class="def-type"><var class="var">val</var> =</code> <strong class="def-name">string_fill_char</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-string_005ffill_005fchar"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-string_005ffill_005fchar-1"><span><code class="def-type"><var class="var">old_val</var> =</code> <strong class="def-name">string_fill_char</strong> <code class="def-code-arguments">(<var class="var">new_val</var>)</code><a class="copiable-link" href="#index-string_005ffill_005fchar-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-string_005ffill_005fchar-2"><span><code class="def-type"><var class="var">old_val</var> =</code> <strong class="def-name">string_fill_char</strong> <code class="def-code-arguments">(<var class="var">new_val</var>, "local")</code><a class="copiable-link" href="#index-string_005ffill_005fchar-2"> ¶</a></span></dt>
<dd><p>Query or set the internal variable used to pad all rows of a character
matrix to the same length.
</p>
<p>The value must be a single character and the default is <code class="code">" "</code> (a
single space). For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">string_fill_char ("X");
[ "these"; "are"; "strings" ]
⇒ "theseXX"
"areXXXX"
"strings"
</pre></div></div>
<p>When called from inside a function with the <code class="code">"local"</code> option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
</p></dd></dl>
<p>Another useful function to control the text justification in this case is
the <code class="code">strjust</code> function.
</p>
<a class="anchor" id="XREFstrjust"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-strjust"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">strjust</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-strjust"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strjust-1"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">strjust</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">pos</var>)</code><a class="copiable-link" href="#index-strjust-1"> ¶</a></span></dt>
<dd><p>Return the text, <var class="var">s</var>, justified according to <var class="var">pos</var>, which may
be <code class="code">"left"</code>, <code class="code">"center"</code>, or <code class="code">"right"</code>.
</p>
<p>If <var class="var">pos</var> is omitted it defaults to <code class="code">"right"</code>.
</p>
<p>Null characters are replaced by spaces. All other character data are
treated as non-white space.
</p>
<p>Example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">strjust (["a"; "ab"; "abc"; "abcd"])
⇒
" a"
" ab"
" abc"
"abcd"
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="Common-String-Operations.html#XREFdeblank">deblank</a>, <a class="ref" href="Searching-and-Replacing-in-Strings.html#XREFstrrep">strrep</a>, <a class="ref" href="Common-String-Operations.html#XREFstrtrim">strtrim</a>, <a class="ref" href="Common-String-Operations.html#XREFuntabify">untabify</a>.
</p></dd></dl>
<p>This shows a problem with character matrices. It simply isn’t possible to
represent strings of different lengths. The solution is to use a cell array of
strings, which is described in <a class="ref" href="Cell-Arrays-of-Strings.html">Cell Arrays of Strings</a>.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="String-Operations.html">String Operations</a>, Previous: <a href="Escape-Sequences-in-String-Constants.html">Escape Sequences in String Constants</a>, Up: <a href="Strings.html">Strings</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>
|