- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

5.1 Creating Strings

The easiest way to create a string is, as illustrated in the introduction, to enclose a text in double-quotes or single-quotes. It is however possible to create a string without actually writing a text. The function blanks creates a string of a given length consisting only of blank characters (ASCII code 32).

Function File: blanks (n)
Return a string of n blanks.

See also repmat

The string representation used by Octave is an array of characters, so the result of blanks(10) is actually a row vector of length 10 containing the value 32 in all places. This lends itself to the obvious generalisation 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.

The easiest way to create a character matrix is to put several strings together into a matrix.

collection = [ "String #1"; "String #2" ];

This creates a 2-by-9 character matrix.

One relevant question is, what happens when 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. While it is possible to use a different character than the blank character using the string_fill_char function, it 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 section 6.2.3 Cell Arrays of Strings.

Built-in Function: char (x)
Built-in Function: char (cell_array)
Built-in Function: char (s1, s2, ...)
Create a string array from a numeric matrix, cell array, or list of

If the argument is a numeric matrix, each element of the matrix is converted to the corresponding ASCII character. For example,

char ([97, 98, 99])
     => "abc"

If the argument is a cell array of strings, the result is a string array with each element corresponding to one element of the cell array.

For multiple string arguments, the result is a string array with each element corresponding to the arguments.

The returned values are padded with blanks as needed to make each row of the string array have the same length.

Function File: strcat (s1, s2, ...)
Return a string containing all the arguments concatenated. For example,

s = [ "ab"; "cde" ];
strcat (s, s, s)
=> "ab ab ab "
        "cdecdecde"

Function File: strvcat (s_1, ..., s_n)
Return a matrix containing the strings (and cell-strings) s_1, ..., s_n as its rows. Each string is padded with blanks in order to form a valid matrix. Unlike str2mat, empty strings are ignored.

See also strcat, str2mat

Function File: strtrunc (s, n)
Truncate the character string s to length n. If s is a char matrix, then the number of columns are adjusted.

If s is a cell array of strings, then the operation is performed on its members and the new cell array is returned.

Built-in Function: val = string_fill_char ()
Built-in Function: old_val = string_fill_char (new_val)
Query or set the internal variable used to pad all rows of a character matrix to the same length. It must be a single character. The default value is " " (a single space). For example,

string_fill_char ("X");
[ "these"; "are"; "strings" ]
     => "theseXX"
        "areXXXX"
        "strings"

Function File: str2mat (s_1, ..., s_n)
Return a matrix containing the strings s_1, ..., s_n as its rows. Each string is padded with blanks in order to form a valid matrix.

This function is modelled after Matlab. In Octave, you can create a matrix of strings by [s_1; ...; s_n] even if the strings are not all the same length.

Built-in Function: ischar (a)
Return 1 if a is a string. Otherwise, return 0.

Function File: s = mat2str (x, n)
Function File: s = mat2str (..., 'class')

Format real/complex numerical matrices as strings. This function returns values that are suitable for the use of the eval function.

The precision of the values is given by n. If n is a scalar then both real and imaginary parts of the matrix are printed to the same precision. Otherwise n (1) defines the precision of the real part and n (2) defines the precision of the imaginary part. The default for n is 17.

If the argument 'class' is given, then the class of x is included in the string in such a way that the eval will result in the construction of a matrix of the same class.

   mat2str( [ -1/3 + i/7; 1/3 - i/7 ], [4 2] )
=> '[-0.3333+0.14i;0.3333-0.14i]'
   mat2str( [ -1/3 +i/7; 1/3 -i/7 ], [4 2] )
=> '[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]'
   mat2str( int16([1 -1]), 'class')
=> 'int16([1,-1])'

See also sprintf, int2str

Function File: num2str (n)
Function File: num2str (x, precision)
Function File: num2str (x, format)
Convert a number to a string. This function is not very flexible. For better control over the results, use sprintf (see section 14.2.4 Formatted Output).

See also sprintf, int2str

Function File: int2str (n)
Convert an integer to a string. This function is not very flexible. For better control over the results, use sprintf (see section 14.2.4 Formatted Output).

See also sprintf, num2str

ISBN 095461206XGNU Octave Manual Version 3See the print edition