Text in String and Character Arrays
There are two ways to represent text in MATLAB®. Starting in R2016b, you can store text in string
arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for
character arrays is to store pieces of text as character vectors. MATLAB displays strings with double
quotes and character vectors with single quotes.
Represent Text with String Arrays
You can store any 1-by-n sequence of characters as a string, using the string data type. Starting in
R2017a, enclose text in double quotes to create a string.
str = "Hello, world"
str =
"Hello, world"
Though the text "Hello, world" is 12 characters long, str itself is a 1-by-1 string, or string scalar.
You can use a string scalar to specify a file name, plot label, or any other piece of textual information.
To find the number of characters in a string, use the strlength function.
n = strlength(str)
n = 12
If the text includes double quotes, use two double quotes within the definition.
str = "They said, ""Welcome!"" and waved."
str =
"They said, "Welcome!" and waved."
To add text to the end of a string, use the plus operator, +. If a variable can be converted to a string,
then plus converts it and appends it.
fahrenheit = 71;
celsius = (fahrenheit-32)/1.8;
tempText = "temperature is " + celsius + "C"
tempText =
"temperature is 21.6667C"
Starting in R2019a, you can also concatenate text using the append function.
tempText2 = append("Today's ",tempText)
tempText2 =
"Today's temperature is 21.6667C"
The string function can convert different types of inputs, such as numeric, datetime, duration, and
categorical values. For example, convert the output of pi to a string.
ps = string(pi)
ps =
"3.1416"
6 Characters and Strings
6-2
There are two ways to represent text in MATLAB®. Starting in R2016b, you can store text in string
arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for
character arrays is to store pieces of text as character vectors. MATLAB displays strings with double
quotes and character vectors with single quotes.
Represent Text with String Arrays
You can store any 1-by-n sequence of characters as a string, using the string data type. Starting in
R2017a, enclose text in double quotes to create a string.
str = "Hello, world"
str =
"Hello, world"
Though the text "Hello, world" is 12 characters long, str itself is a 1-by-1 string, or string scalar.
You can use a string scalar to specify a file name, plot label, or any other piece of textual information.
To find the number of characters in a string, use the strlength function.
n = strlength(str)
n = 12
If the text includes double quotes, use two double quotes within the definition.
str = "They said, ""Welcome!"" and waved."
str =
"They said, "Welcome!" and waved."
To add text to the end of a string, use the plus operator, +. If a variable can be converted to a string,
then plus converts it and appends it.
fahrenheit = 71;
celsius = (fahrenheit-32)/1.8;
tempText = "temperature is " + celsius + "C"
tempText =
"temperature is 21.6667C"
Starting in R2019a, you can also concatenate text using the append function.
tempText2 = append("Today's ",tempText)
tempText2 =
"Today's temperature is 21.6667C"
The string function can convert different types of inputs, such as numeric, datetime, duration, and
categorical values. For example, convert the output of pi to a string.
ps = string(pi)
ps =
"3.1416"
6 Characters and Strings
6-2
