String array
Starting in R2016b, you can represent text using string
arrays instead of character 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 strings 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 by enclosing a piece of text in double quotes. Double quotes were introduced in R2017a.
str = "Hello, world"
str = "Hello, world"
One way to create a string array is to concatenate strings into an array 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.