Represent Text with Character and String Arrays

There are two ways to represent text in MATLAB®. You can store text in character arrays. A typical use is to store short pieces of text as character vectors. And starting in R2016b, you can also store multiple pieces of text in string arrays. String arrays provide a set of functions for working with text as data.

Represent Text with Character Vectors

Create a character vector by enclosing a sequence of characters in single quotation marks. MATLAB® displays character vectors using single quotation marks.

chr = 'Hello, world'
chr = 
'Hello, world'

Character vectors store characters as 1-by-n vectors. You can index directly into character vectors to get characters, or to change them.

chr(1:5)
ans = 
'Hello'
chr(1:5) = 'HELLO';
chr
chr = 
'HELLO, world'

You can work with character vectors just as you would with arrays of any other type. For example, you can concatenate character vectors.

street = '123 Maple St.';
city = 'Lakeview, MA 01234';
fullAddress = [street ', ' city]
fullAddress = 
'123 Maple St., Lakeview, MA 01234'

Typical uses for character vectors include specifying file names, plot labels, or input arguments for functions. For more information on character arrays, see Create Character Arrays.

Represent Text with String Arrays

You also can store text in string arrays. Each element of a string array stores a 1-by-n character vector.

Starting in R2017a, you can create strings using double quotes. MATLAB® displays strings with double quotes.

str = "Welcome, friend"
str = 
"Welcome, friend"

As an alternative, you can convert a character vector to a string using the string function.

chr = 'Hello, world';
str = string(chr)
str = 
"Hello, world"

str is a 1-by-1 string, or string scalar. To find the number of characters in a string, use the strlength function.

whos str
  Name      Size            Bytes  Class     Attributes

  str       1x1               168  string              
strlength(str)
ans = 12

You can store multiple pieces of text in a string array. Each element of the array can contain a string of a different size.

str = ["Mercury","Gemini","Apollo";...
       "Skylab","Skylab B","ISS"]
str = 2x3 string array
    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS"   

str is a 2-by-3 string array. You can find the lengths of the strings with the strlength function.

whos str
  Name      Size            Bytes  Class     Attributes

  str       2x3               470  string              
L = strlength(str)
L = 2×3

     7     6     6
     6     8     3

As an alternative, you also can convert a cell array of character vectors to a string array using the string function.

C = {'Mercury','Venus','Earth'};
str = string(C)
str = 1x3 string array
    "Mercury"    "Venus"    "Earth"

Use string arrays to store and work with multiple pieces of text. You can find and replace substrings, sort and reshape string arrays, and work with text as data. For more information on string arrays, see Create String Arrays.

See Also

| | |

Related Topics