MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Cell Arrays of Character Vectors


Convert to Cell Array of Character Vectors


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.

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. For more information, see “Represent Text with Character and String Arrays” on
page 6-2.

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 =

Cell Arrays of Character Vectors
Free download pdf