C = {'Mercury','Venus','Earth'}
C = 1x3 cell
{'Mercury'}
{'Venus'}
{'Earth'}
str = string(C)
str = 1x3 string
"Mercury"
"Venus"
"Earth"
In addition to character vectors, you can convert numeric, datetime, duration, and categorical values
to strings using the string function.
Convert a numeric array to a string array.
X = [5 10 20 3.1416];
string(X)
ans = 1x4 string
"5"
"10"
"20"
"3.1416"
Convert a datetime value to a string.
d = datetime('now');
string(d)
ans =
"01-Sep-2021 16:04:06"
Also, you can read text from files into string arrays using the readtable, textscan, and fscanf
functions.
Create Empty and Missing Strings
String arrays can contain both empty and missing values. An empty string contains zero characters.
When you display an empty string, the result is a pair of double quotes with nothing between them
(""). The missing string is the string equivalent to NaN for numeric arrays. It indicates where a string
array has missing values. When you display a missing string, the result is, with no
quotation marks.
Create an empty string array using the strings function. When you call strings with no
arguments, it returns an empty string. Note that the size of str is 1-by-1, not 0-by-0. However, str
contains zero characters.
str = strings
str =
""
Create an empty character vector using single quotes. Note that the size of chr is 0-by-0.
chr = ''
chr =
0x0 empty char array
6 Characters and Strings
6-6
C = 1x3 cell
{'Mercury'}
{'Venus'}
{'Earth'}
str = string(C)
str = 1x3 string
"Mercury"
"Venus"
"Earth"
In addition to character vectors, you can convert numeric, datetime, duration, and categorical values
to strings using the string function.
Convert a numeric array to a string array.
X = [5 10 20 3.1416];
string(X)
ans = 1x4 string
"5"
"10"
"20"
"3.1416"
Convert a datetime value to a string.
d = datetime('now');
string(d)
ans =
"01-Sep-2021 16:04:06"
Also, you can read text from files into string arrays using the readtable, textscan, and fscanf
functions.
Create Empty and Missing Strings
String arrays can contain both empty and missing values. An empty string contains zero characters.
When you display an empty string, the result is a pair of double quotes with nothing between them
(""). The missing string is the string equivalent to NaN for numeric arrays. It indicates where a string
array has missing values. When you display a missing string, the result is
quotation marks.
Create an empty string array using the strings function. When you call strings with no
arguments, it returns an empty string. Note that the size of str is 1-by-1, not 0-by-0. However, str
contains zero characters.
str = strings
str =
""
Create an empty character vector using single quotes. Note that the size of chr is 0-by-0.
chr = ''
chr =
0x0 empty char array
6 Characters and Strings
6-6
