Main Content

cell2mat

Convert cell array to ordinary array

Description

A = cell2mat(C) converts a cell array to an ordinary array.

The contents of C must support concatenation into an N-dimensional rectangle. Otherwise, the results are undefined. For example, the contents of cells in the same column must have the same number of columns, although they need not have the same number of rows.

Cell array with six cells concatenated into one ordinary array

example

Examples

collapse all

Convert numeric arrays in four cells of a cell array into one numeric array.

C = {[1],    [2 3 4];
     [5; 9], [6 7 8; 10 11 12]}
C=2×2 cell array
    {[       1]}    {[   2 3 4]}
    {2×1 double}    {2×3 double}

A = cell2mat(C)
A = 3×4

     1     2     3     4
     5     6     7     8
     9    10    11    12

Convert structures in a cell array into one structure array. The structures must have the same fields.

s1.a = [1 2 3 4];
s1.b = 'Good';
s2.a = [5 6; 7 8];
s2.b = 'Morning';
c = {s1,s2};
d = cell2mat(c)
d=1×2 struct array with fields:
    a
    b

Display the first field of structure d(1).

d(1).a
ans = 1×4

     1     2     3     4

Display the second field of d(2).

d(2).b
ans = 
'Morning'

Since R2025a

Convert a cell array containing different data types to an ordinary array.

Create a cell array that contains single and double values.

a = single([1 2 3]);
b = double([2 4 6]);
C = {a;b}
C =

  2×1 cell array

    {[1 2 3]}
    {[2 4 6]}

Convert the cell array to a numeric array. The resulting array is single.

D = cell2mat(C)
D =

  2×3 single matrix

     1     2     3
     2     4     6

Input Arguments

collapse all

Input cell array. If the contents of the cells all have the same data type, the resulting array has that data type. The function can concatenate cells that contain structures if those structures have the same field names.

The cells can have different data types if MATLAB® can convert the data types to one output type. For examples of basic data type combinations and their resulting types, see Valid Combinations of Unlike Classes.

cell2mat also accepts cell arrays containing class-based objects. For information on how class-based objects can be combined, see Concatenating Objects of Different Classes.

cell2mat does not accept nested cells within C.

Extended Capabilities

expand all

Version History

Introduced before R2006a

expand all