Access Data in Cell Array

A cell array is a data type with indexed data containers called cells. Each cell can contain any type of data. Cell arrays are often used to hold data from a file that has inconsistent formatting, such as columns that contain both numeric and text data.

For instance, consider a 2-by-3 cell array of mixed data.

C = 'one','two','three'; 100,200,rand(3,3)>
C=2×3 cell array

Each element is within a cell. If you index into this array using standard parentheses, the result is a subset of the cell array that includes the cells.

C2 = C(1:2,1:2)
C2=2×2 cell array

To read or write the contents within a specific cell, enclose the indices in curly braces.

R = C
R = 3×3 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575
C = 'longer text in a third location'
C=2×3 cell array

To replace the contents of multiple cells at the same time, use parentheses to refer to the cells and curly braces to define an equivalently sized cell array.

C(1,1:2) = 'first','second'>
C=2×3 cell array <[ 100]> <[ 200]>

Read Data from Multiple Cells

Most of the data processing functions in MATLAB® operate on a rectangular array with a uniform data type. Because cell arrays can contain a mix of types and sizes, you sometimes must extract and combine data from cells before processing that data. This section describes a few common scenarios.

Text in Specific Cells

When the entire cell array or a known subset of cells contains text, you can index and pass the cells directly to any of the text processing functions in MATLAB. For instance, find where the letter t appears in each element of the first row of C .

ts = strfind(C(1,:),'t')
ts=1×3 cell array

Numbers in Specific Cells

The two main ways to process numeric data in a cell array are:

To combine numeric cells, use the cell2mat function. The arrays in each cell must have compatible sizes for concatenation. For instance, the first two elements of the second row of C are scalar values. Combine them into a 1-by-2 numeric vector.

v = cell2mat(C(2,1:2))
v = 1×2 100 200

To process individual cells, you can use the cellfun function. When calling cellfun , specify the function to apply to each cell. Use the @ symbol to indicate that it is a function and to create a function handle. For instance, find the length of each of the cells in the second row of C .

len = cellfun(@length,C(2,:))
len = 1×3 1 1 3

Data in Cells with Unknown Indices

When some of the cells contain data that you want to process, but you do not know the exact indices, you can use one of these options:

For instance, suppose you want to process only the cells that contain character vectors. To take advantage of logical indexing, first use the cellfun function with ischar to find those cells.

idx = cellfun(@ischar,C)
idx = 2x3 logical array 1 1 1 0 0 0

Then, use the logical array to index into the cell array, C(idx) . The result of the indexing operation is a column vector, which you can pass to a text processing function, such as strlength .

len = strlength(C(idx))
len = 3×1 5 6 31

The other approach is to use a loop to check and process the contents of each cell. For instance, find cells that contain the letter t and combine them into a string array by looping through the cells. Track how many elements the loop adds to the string array in variable n .

n = 0; for k = 1:numel(C) if ischar(C) && contains(C,"t") n = n + 1; txt(n) = string(C); end end txt
txt = 1x2 string "first" "longer text in a third location"

Index into Multiple Cells

If you refer to multiple cells using curly brace indexing, MATLAB returns the contents of the cells as a comma-separated list . For example,

Because each cell can contain a different type of data, you cannot assign this list to a single variable. However, you can assign the list to the same number of variables as cells.