Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

MATLAB Cell Array: Create, Index, cellfun and cell2mat

MATLAB cell array how-to: create with braces, index with () vs {}, preallocate with cell, process with cellfun, and flatten with cell2mat.

Written and maintained by CASRAI Editorial Board

Last updated

A MATLAB cell array is a container whose cells can each hold data of a different type or size. You create one with curly braces, read the container with parentheses, read the contents with curly braces, and process it with cellfun or flatten it with cell2mat. This guide follows that one workflow end to end, using the behaviour described in the MathWorks documentation (read 2026-11-07). It is written for researchers and analysts who meet cell arrays when a script mixes text labels, scalars and matrices in one variable.

What a MATLAB cell array is used for in research

An ordinary numeric array holds one data type in a rectangular grid. A cell array relaxes that: in the MathWorks example the array C = {'one','two','three'; 100,200,rand(3,3)} holds three character vectors, two scalars and a 3-by-3 matrix in a single 2-by-3 variable. Typical research uses are lists of file names, ragged results (one vector per subject, each of a different length) and labelled records collected in a loop. Variants of the search term, such as “cell array matlab”, “cell arrays in matlab” and “matlab array of cells”, all describe this same data type.

If you want named fields rather than numbered positions, see Structure in MATLAB, which covers structs and struct arrays and compares them with cell arrays and tables.

Access

Cell arrays are part of base MATLAB; no toolbox is needed for the functions in this guide (cell, cellfun, cell2mat). MATLAB is obtained from MathWorks (mathworks.com); GNU Octave is an alternative covered in the table below.

Create a cell array

  1. Enclose the values in curly braces, separating columns with commas or spaces and rows with semicolons: C = {'one','two','three'; 100,200,rand(3,3)}.
  2. Check the result with size(C) (here 2-by-3). Each cell is addressed by its row and column, like a matrix element.

Index a cell array: parentheses versus curly braces

Parentheses return a subset of the cell array; curly braces return the contents of a cell. The MathWorks page “Access Data in a Cell Array” states that indexing with standard parentheses returns a subset of the array that includes the cells, and that to read or write the contents within a specific cell you enclose the indices in curly braces.

C = {'one','two','three'; 100,200,rand(3,3)};C2 = C(1:2,1:2)   % parentheses: a 2-by-2 cell arrayR  = C{2,3}       % curly braces: the 3-by-3 double stored in that cell

Assign to cells

  • Replace the contents of one cell with curly braces: C{1,3} = 'longer text in a third location'.
  • Replace several cells at once with parentheses and an equally sized cell array on the right: C(1,1:2) = {'first','second'}.

For linear, logical and subscript indexing on ordinary arrays, see MATLAB Indexing.

Preallocate an empty cell array

The cell function creates a cell array of empty matrices. C = cell(n) returns an n-by-n array; cell(sz1,...,szN) and cell(sz) (a size vector) give other shapes; cell(3) gives a 3-by-3 array in which each cell is a 0-by-0 double. The MathWorks reference describes C = cell(3,4,2) as equivalent to C{3,4,2} = [], and treats negative sizes as 0. Use it to preallocate an empty cell array before filling it in a loop.

Apply a function to every cell with cellfun

The syntax is A = cellfun(func,C). With the default UniformOutput value of true, func must return scalars of the same data type, and A is an ordinary array. Set 'UniformOutput',false when the results are not scalars; A is then a cell array: cellfun(@mean,C,'UniformOutput',false). Multiple outputs work too: [nrows,ncols] = cellfun(@size,C). If you are writing the function inline, anonymous functions with @(x) are the usual way.

Worked example

From the MathWorks cellfun page: C = {1:10, [2; 4; 6], []}; A = cellfun(@mean,C) returns 5.5000 4.0000 NaN. The empty cell gives NaN because the mean of an empty matrix is NaN, so an empty cell is not skipped and can propagate into later calculations.

Convert a cell array to a matrix with cell2mat

A = cell2mat(C) concatenates the cell contents into one array. The contents must be able to be concatenated into an N-D rectangle, otherwise the results are undefined; cells in the same column must have the same number of columns. Per the documentation, since R2025a mixed types are allowed if they can be converted to one type, while earlier releases required all contents to be the same type. Structs are accepted if they have the same field names; nested cell arrays are not accepted.

C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};A = cell2mat(C)   % 3-by-4: [1 2 3 4; 5 6 7 8; 9 10 11 12]

Output and how to read it

  • A cell array displays as a grid in braces; a nested value is shown as its size and class (for example a 3-by-3 double) rather than its numbers. Use C{2,3} to see the numbers.
  • cellfun with uniform output returns a plain array of the same shape as C; with 'UniformOutput',false it returns another cell array.
  • cell2mat returns a single array whose size is the sum of the block sizes.

Troubleshooting

Symptom Cause and fix (per the MathWorks reference pages)
cellfun fails when your function returns vectors or text With UniformOutput true the function must return scalars of one data type. Add 'UniformOutput',false.
cellfun stops at one bad cell Without an ErrorHandler, the error is rethrown. Pass a function handle; its first input is a struct with the fields identifier, message and index.
cell2mat gives odd or undefined output The contents cannot form a rectangle. Check that cells in the same column have the same number of columns, and that none is itself a cell array.
NaN appears in results An empty cell, as in the worked example above. Remove or fill empty cells first.
You get a 1-by-1 cell instead of the value You used parentheses. Use curly braces to get the contents.

Free alternatives

Tool Cell arrays
GNU Octave Octave’s manual (docs.octave.org, read 2026-11-07) creates them with braces, c = {"a string", rand(2, 2)};, and indexes them with the { } operators, for example c{1}, which also insert or overwrite elements, as in c{3} = 3.

FAQ

What is the difference between an array and a cell array in MATLAB?

A numeric array holds one data type; a cell array holds values of different types and sizes, one per cell. The MathWorks example mixes text, scalars and a 3-by-3 matrix in one variable.

How do I index a cell array?

Use parentheses, C(1:2,1:2), to get a smaller cell array, and curly braces, C{2,3}, to get what is stored inside.

How do I create an empty cell array?

C = cell(3) returns a 3-by-3 cell array in which each cell is an empty 0-by-0 double.

How do I get the size or length of a cell array?

size(C) returns the dimensions, and cellfun(@size,C) style calls report the size of each cell’s contents, as in [nrows,ncols] = cellfun(@size,C).

How do I convert a cell array to a matrix?

Use cell2mat(C) when all contents can be concatenated into a rectangle; nested cell arrays are not accepted.

What does cellfun do?

It applies a function to the contents of each cell and returns the results as an array, or as a cell array when 'UniformOutput' is false.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Cell Array: Create, Index, cellfun and cell2mat

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →