cell2mat
Convert cell array to ordinary array
Syntax
Description
A = cell2mat(
converts a cell array to an ordinary
array.C
)
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.
Examples
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
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
The
cell2mat
function supports tall arrays with the following usage
notes and limitations:
The cells of the input cell array must contain numeric, logical, or character data types.
For more information, see Tall Arrays.
Usage notes and limitations:
Code generation is not supported for
cell2mat
for unbounded or variable-size cell arrays containing sparse arrays.Arrays or structures present in unbounded or variable-size cell array input with a dimension greater than 2 are not supported for code generation with the
cell2mat
function.For a run-time empty-cell input,
cell2mat
returns an empty cell of the same data type. For a compile-time empty-cell input,cell2mat
returns an empty cell of typedouble
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The cell2mat
function fully supports cell arrays
containing gpuArray
(Parallel Computing Toolbox) objects. For more
information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aBefore R2025a, cell2mat
accepted numeric, logical, and
character types, as well as structures with the same field names. All cells of the
input array had to have the same data type.
cell2mat
can now accept cell arrays containing additional
data types, including strings, categoricals, gpuArray
objects, and
class-based objects.
The function can also convert a cell array that contains different data types if MATLAB can convert those data types to one output type. For examples of basic data type combinations and their resulting types, see Valid Combinations of Unlike Classes.
If the cell array contains class-based objects of different classes, MATLAB attempts to concatenate them. For more information, see Concatenating Objects of Different Classes.
See Also
mat2cell
| num2cell
| cell
| cell2table
| cell2struct
| struct2cell
| table2cell
| iscell
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)