When you create character arrays from character vectors, all of the vectors must have the same length. This often means that you have to pad blanks at the end of character vectors to equalize their length. However, another type of MATLAB® array, the cell array, can hold different sizes and types of data in an array without padding. A cell array of character vectors is a cell array where every cell contains a character vector. Cell arrays of character vectors provide a flexible way to store character vectors of varying lengths.
Convert a character array to a cell array of character vectors.
data
is padded with spaces so that each row has an equal number of
characters. Use cellstr
to convert the character array.
data = ['Allison Jones';'Development ';'Phoenix ']; celldata = cellstr(data)
celldata = 3×1 cell array {'Allison Jones'} {'Development' } {'Phoenix' }
data
is a 3
-by-13
character
array, while celldata
is a 3
-by-1
cell array of character vectors. cellstr
also strips the blank spaces
at the ends of the rows of data
.
The iscellstr
function determines if the input
argument is a cell array of character vectors. It returns a logical 1
(true
) in the case of celldata
:
iscellstr(celldata)
ans = logical 1
Use char
to convert back to a padded character
array.
chr = char(celldata)
chr = 3×13 char array 'Allison Jones' 'Development ' 'Phoenix '
For more information on cell arrays, see Access Data in Cell Array.
While the phrase "cell array of strings" frequently has been used to describe such
arrays, the phrase is not accurate because such a cell array holds character vectors, not
strings. Starting in R2016b, MATLAB provides string arrays as another means of storing
text. If you create variables that have the string
data type, store
them in string arrays, not cell arrays.
This table describes the MATLAB functions for working with cell arrays of character vectors.
Function |
Description |
---|---|
Convert a character array to a cell array of character vectors. | |
Convert a cell array of character vectors to a character array. | |
Remove trailing blanks from a character array. | |
Return | |
Sort elements in ascending or descending order. | |
Concatenate character arrays or cell arrays of character arrays. | |
Compare character arrays or cell arrays of character arrays. |
You can also use the following set
functions with cell arrays of
character vectors.