Cell Arrays of Character Vectors
To store text as a character vector, enclose it single quotes. Typically, a character vector has text that you consider to be a single piece of information, such as a file name or a label for a plot. If you have many pieces of text, such as a list of file names, then you can store them in a cell array. A cell array whose elements are all character vectors is a cell array of character vectors.
Note
The recommended way to store text is to use string arrays. If you create variables that have the
string
data type, store them in string arrays, not cell arrays. For more information, see Text in String and Character Arrays and Update Your Code to Accept Strings.While the phrase cell array of strings frequently has been used to describe such cell arrays, the phrase is no longer accurate because such a cell array holds character vectors, not strings.
Create Cell Array of Character Vectors
To create a cell array of character vectors, use curly braces, {}
, just as you would to create any cell array. For example, use a cell array of character vectors to store a list of names.
C = {'Li','Sanchez','Jones','Yang','Larson'}
C = 1x5 cell
{'Li'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}
The character vectors in C
can have different lengths because a cell array does not require that its contents have the same size. To determine the lengths of the character vectors in C
, use the strlength
function.
L = strlength(C)
L = 1×5
2 7 5 4 6
Access Character Vectors in Cell Array
To access character vectors in a cell array, index into it using curly braces, {}
. Extract the contents of the first cell and store it as a character vector.
C = {'Li','Sanchez','Jones','Yang','Larson'}; chr = C{1}
chr = 'Li'
Assign a different character vector to the first cell.
C{1} = 'Yang'
C = 1x5 cell
{'Yang'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}
To refer to a subset of cells, instead of their contents, index using smooth parentheses.
C(1:3)
ans = 1x3 cell
{'Yang'} {'Sanchez'} {'Jones'}
While you can access the contents of cells by indexing, most functions that accept cell arrays as inputs operate on the entire cell array. For example, you can use the strcmp
function to compare the contents of C
to a character vector. strcmp
returns 1
where there is a match and 0
otherwise.
TF = strcmp(C,'Yang')
TF = 1x5 logical array
1 0 0 1 0
You can sum over TF
to find the number of matches.
num = sum(TF)
num = 2
Use TF
as logical indices to return the matches in C
. If you index using smooth parentheses, then the output is a cell array containing only the matches.
M = C(TF)
M = 1x2 cell
{'Yang'} {'Yang'}
Convert Cell Arrays to String Arrays
String arrays are supported throughout MATLAB® and MathWorks® products. Therefore it is recommended that you use string arrays instead of cell arrays of character vectors. (However, MATLAB functions that accept string arrays as inputs do accept character vectors and cell arrays of character vectors as well.)
You can convert cell arrays of character vectors to string arrays. To convert a cell array of character vectors, use the string
function.
C = {'Li','Sanchez','Jones','Yang','Larson'}
C = 1x5 cell
{'Li'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}
str = string(C)
str = 1x5 string
"Li" "Sanchez" "Jones" "Yang" "Larson"
In fact, the string
function converts any cell array, so long as all of the contents can be converted to strings.
C2 = {5, 10, 'some text', datetime('today')}
C2=1×4 cell array
{[5]} {[10]} {'some text'} {[05-Sep-2024]}
str2 = string(C2)
str2 = 1x4 string
"5" "10" "some text" "05-Sep-2024"
See Also
cellstr
| char
| iscellstr
| strcmp
| string