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
As of R2018b, 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)
Cell Arrays of Character Vectors
6-13
Précédent

- 195/1450

Suivant