<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Character Arrays (GNU Octave (version 5.1.0))</title> <meta name="description" content="Character Arrays (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Character Arrays (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Strings.html#Strings" rel="up" title="Strings"> <link href="Creating-Strings.html#Creating-Strings" rel="next" title="Creating Strings"> <link href="Escape-Sequences-in-String-Constants.html#Escape-Sequences-in-String-Constants" rel="prev" title="Escape Sequences in String Constants"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Character-Arrays"></a> <div class="header"> <p> Next: <a href="Creating-Strings.html#Creating-Strings" accesskey="n" rel="next">Creating Strings</a>, Previous: <a href="Escape-Sequences-in-String-Constants.html#Escape-Sequences-in-String-Constants" accesskey="p" rel="prev">Escape Sequences in String Constants</a>, Up: <a href="Strings.html#Strings" 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#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Character-Arrays-1"></a> <h3 class="section">5.2 Character Arrays</h3> <p>The string representation used by Octave is an array of characters, so internally the string <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>"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">collection = [ "String #1"; "String #2" ]; </pre></div> <p>This creates a 2-by-9 character matrix. </p> <p>The function <code>ischar</code> can be used to test if an object is a character matrix. </p> <a name="XREFischar"></a><dl> <dt><a name="index-ischar"></a><em></em> <strong>ischar</strong> <em>(<var>x</var>)</em></dt> <dd><p>Return true if <var>x</var> is a character array. </p> <p><strong>See also:</strong> <a href="Predicates-for-Numeric-Objects.html#XREFisfloat">isfloat</a>, <a href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a href="Predicates-for-Numeric-Objects.html#XREFislogical">islogical</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisnumeric">isnumeric</a>, <a href="#XREFisstring">isstring</a>, <a href="Cell-Arrays-of-Strings.html#XREFiscellstr">iscellstr</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>. </p></dd></dl> <a name="XREFisstring"></a><dl> <dt><a name="index-isstring"></a><em></em> <strong>isstring</strong> <em>(<var>s</var>)</em></dt> <dd><p>Return true if <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>{}</samp>’ to access elements rather than string arrays which use ordinary array indexing ‘<samp>()</samp>’. </p> <p>Programming Note: Octave does not yet implement string arrays so this function will always return false. </p> <p><strong>See also:</strong> <a href="#XREFischar">ischar</a>, <a href="Cell-Arrays-of-Strings.html#XREFiscellstr">iscellstr</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisfloat">isfloat</a>, <a href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a href="Predicates-for-Numeric-Objects.html#XREFislogical">islogical</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisnumeric">isnumeric</a>, <a 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>ischar</code> function in combination with the <code>isrow</code> function as in the following example: </p> <div class="example"> <pre class="example">ischar (collection) ⇒ 1 ischar (collection) && isrow (collection) ⇒ 0 ischar ("my string") && isrow ("my string") ⇒ 1 </pre></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>string_fill_char</code> function. </p> <a name="XREFstring_005ffill_005fchar"></a><dl> <dt><a name="index-string_005ffill_005fchar"></a><em><var>val</var> =</em> <strong>string_fill_char</strong> <em>()</em></dt> <dt><a name="index-string_005ffill_005fchar-1"></a><em><var>old_val</var> =</em> <strong>string_fill_char</strong> <em>(<var>new_val</var>)</em></dt> <dt><a name="index-string_005ffill_005fchar-2"></a><em></em> <strong>string_fill_char</strong> <em>(<var>new_val</var>, "local")</em></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>" "</code> (a single space). For example: </p> <div class="example"> <pre class="example">string_fill_char ("X"); [ "these"; "are"; "strings" ] ⇒ "theseXX" "areXXXX" "strings" </pre></div> <p>When called from inside a function with the <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>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 href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings">Cell Arrays of Strings</a>. </p> <hr> <div class="header"> <p> Next: <a href="Creating-Strings.html#Creating-Strings" accesskey="n" rel="next">Creating Strings</a>, Previous: <a href="Escape-Sequences-in-String-Constants.html#Escape-Sequences-in-String-Constants" accesskey="p" rel="prev">Escape Sequences in String Constants</a>, Up: <a href="Strings.html#Strings" 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#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>