Next: Comparing Strings, Up: 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).
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 Cell Arrays of Strings.
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.
Return a string containing all the arguments concatenated. For example,
s = [ "ab"; "cde" ]; strcat (s, s, s) => "ab ab ab " "cdecdecde"
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.
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.
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"
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.
Format real/complex numerial 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.
Convert a number to a string. This function is not very flexible. For better control over the results, use
sprintf
(see Formatted Output).See also: sprintf, int2str.
Convert an integer to a string. This function is not very flexible. For better control over the results, use
sprintf
(see Formatted Output).See also: sprintf, num2str.