Create String Arrays
String arrays were introduced in R2016b. String arrays store pieces of text and provide a set of
functions for working with text as data. You can index into, reshape, and concatenate strings arrays
just as you can with arrays of any other type. You also can access the characters in a string and
append text to strings using the plus operator. To rearrange strings within a string array, use
functions such as split, join, and sort.
Create String Arrays from Variables
MATLAB® provides string arrays to store pieces of text. Each element of a string array contains a 1by-n sequence of characters.
Starting in R2017a, you can create a string using double quotes.
str = "Hello, world"
str =
"Hello, world"
As an alternative, you can convert a character vector to a string using the string function. chr is a
1-by-17 character vector. str is a 1-by-1 string that has the same text as the character vector.
chr = 'Greetings, friend'
chr =
'Greetings, friend'
str = string(chr)
str =
"Greetings, friend"
Create a string array containing multiple strings using the [] operator. str is a 2-by-3 string array
that contains six strings.
str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"]
str = 2x3 string
"Mercury"
"Gemini"
"Apollo"
"Skylab"
"Skylab B"
"ISS"
Find the length of each string in str with the strlength function. Use strlength, not length, to
determine the number of characters in strings.
L = strlength(str)
L = 2×3
7
6
6
6
8
3
As an alternative, you can convert a cell array of character vectors to a string array using the string
function. MATLAB displays strings in string arrays with double quotes, and displays characters
vectors in cell arrays with single quotes.
Create String Arrays
6-5
String arrays were introduced in R2016b. String arrays store pieces of text and provide a set of
functions for working with text as data. You can index into, reshape, and concatenate strings arrays
just as you can with arrays of any other type. You also can access the characters in a string and
append text to strings using the plus operator. To rearrange strings within a string array, use
functions such as split, join, and sort.
Create String Arrays from Variables
MATLAB® provides string arrays to store pieces of text. Each element of a string array contains a 1by-n sequence of characters.
Starting in R2017a, you can create a string using double quotes.
str = "Hello, world"
str =
"Hello, world"
As an alternative, you can convert a character vector to a string using the string function. chr is a
1-by-17 character vector. str is a 1-by-1 string that has the same text as the character vector.
chr = 'Greetings, friend'
chr =
'Greetings, friend'
str = string(chr)
str =
"Greetings, friend"
Create a string array containing multiple strings using the [] operator. str is a 2-by-3 string array
that contains six strings.
str = ["Mercury","Gemini","Apollo";
"Skylab","Skylab B","ISS"]
str = 2x3 string
"Mercury"
"Gemini"
"Apollo"
"Skylab"
"Skylab B"
"ISS"
Find the length of each string in str with the strlength function. Use strlength, not length, to
determine the number of characters in strings.
L = strlength(str)
L = 2×3
7
6
6
6
8
3
As an alternative, you can convert a cell array of character vectors to a string array using the string
function. MATLAB displays strings in string arrays with double quotes, and displays characters
vectors in cell arrays with single quotes.
Create String Arrays
6-5
