String array
You can represent text in MATLAB® using string arrays. Each element of a string array
stores a sequence of characters. The sequences can have different lengths without
padding, such as "yes"
and "no"
. A string array
that has only one element is also called a string
scalar.
You can index into, reshape, and concatenate string arrays using standard array
operations, and you can append text to them using the +
operator. If
a string array represents numbers, then you can convert it to a numeric array using the
double
function.
You can create a string scalar by enclosing a piece of text in double quotes.
str = "Hello, world"
str = "Hello, world"
To create a string array, you can concatenate string scalars using square brackets, just as you can concatenate numbers into a numeric array.
str = ["Mercury" "Gemini" "Apollo"; "Skylab" "Skylab B" "ISS"]
str = 2x3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"
You also can convert variables of different data types into string arrays using the
string
function, described below.
For a list of functions to create and manipulate text in string arrays, see Characters and Strings.
If the input argument is an object, then it must belong to a class that
implements a string
method to represent the object as a
string.
Converting a char
array to
a numeric type will produce an array of the corresponding Unicode code values. Text in strings does not convert in this way.
Converting a string that does not represent a single numeric value to
double
will produce a NaN
result. For
more information, see Unicode and ASCII Values.