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) |
6.2.3 Cell Arrays of Strings
One common use of cell arrays is to store multiple strings in the same
variable. It is 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. If, however, the character
matrix representation is required for an operation, it can be converted
to a cell array of strings using the cellstr
function
a = ["hello"; "world"]; c = cellstr (a) => c = { [1,1] = hello [2,1] = world }
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 strcmp
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 the string
argument,
c = {"hello", "world"}; strcmp ("hello", c) => ans = 1 0
The following functions for string manipulation support cell arrays of
strings, strcmp
, strcmpi
, strncmp
, strncmpi
,
str2double
, str2mat
, strappend
, strtrunc
,
strvcat
, strfind
, and strmatch
.
- Built-in Function: cellstr (string)
- Create a new cell array object from the elements of the string array string.
- Built-in Function: iscellstr (cell)
- Return true if every element of the cell array cell is a character string
- Function File: [idxvec, errmsg] = cellidx (listvar, strlist)
- Return indices of string entries in listvar that match strings
in strlist.
Both listvar and strlist may be passed as strings or string matrices. If they are passed as string matrices, each entry is processed by
deblank
prior to searching for the entries.The first output is the vector of indices in listvar.
If strlist contains a string not in listvar, then an error message is returned in errmsg. If only one output argument is requested, then cellidx prints errmsg to the screen and exits with an error.
ISBN 095461206X | GNU Octave Manual Version 3 | See the print edition |