Access Data in Cell Array
This example shows how to read and write data to and from a cell array.
Create a 2-by-3 cell array of text and numeric data.
C = {'one', 'two', 'three';
1, 2, 3}
C=2×3 cell array
{'one'}
{'two'}
{'three'}
{[ 1]}
{[ 2]}
{[
3]}
There are two ways to refer to the elements of a cell array. Enclose indices in smooth parentheses,
(), to refer to sets of cells--for example, to define a subset of the array. Enclose indices in curly
braces, {}, to refer to the text, numbers, or other data within individual cells.
Cell Indexing with Smooth Parentheses, ()
Cell array indices in smooth parentheses refer to sets of cells. For example, to create a 2-by-2 cell
array that is a subset of C, use smooth parentheses.
upperLeft = C(1:2,1:2)
upperLeft=2×2 cell array
{'one'}
{'two'}
{[ 1]}
{[ 2]}
Update sets of cells by replacing them with the same number of cells. For example, replace cells in
the first row of C with an equivalent-sized (1-by-3) cell array.
C(1,1:3) = {'first','second','third'}
C=2×3 cell array
{'first'}
{'second'}
{'third'}
{[
1]}
{[
2]}
{[
3]}
If cells in your array contain numeric data, you can convert the cells to a numeric array using the
cell2mat function.
numericCells = C(2,1:3)
numericCells=1×3 cell array
{[1]}
{[2]}
{[3]}
numericVector = cell2mat(numericCells)
numericVector = 1×3
1
2
3
numericCells is a 1-by-3 cell array, but numericVector is a 1-by-3 array of type double.
Access Data in Cell Array
12-5
Précédent

- 541/1450

Suivant