Cell array
expand all in page
Description
A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text, combinations of text and numbers, or numeric arrays of different sizes. Refer to sets of cells by enclosing indices in smooth parentheses, ()
. Access the contents of cells by indexing with curly braces, {}
.
Creation
When you have data to put into a cell array, create the array using the cell array construction operator, {}
.
C = {1,2,3; 'text',rand(5,10,2),{11; 22; 33}}
C=2×3 cell array {[ 1]} {[ 2]} {[ 3]} {'text'} {5x10x2 double} {3x1 cell}
You also can use {}
to create an empty 0-by-0 cell array.
C = {}
C = 0x0 empty cell array
To create a cell array with a specified size, use the cell
function, described below.
You can use cell
to preallocate a cell array to which you assign data later. cell
also converts certain types of Java®, .NET, and Python® data structures to cell arrays of equivalent MATLAB® objects.
Syntax
C = cell(n)
C = cell(sz1,...,szN)
C = cell(sz)
D = cell(obj)
Description
example
C = cell(n)
returns an n
-by-n
cell array of empty matrices.
example
C = cell(sz1,...,szN)
returns a sz1
-by-...-by-szN
cell array of empty matrices where sz1,...,szN
indicate the size of each dimension. For example, cell(2,3)
returns a 2-by-3 cell array.
example
C = cell(sz)
returns a cell array of empty matrices where size vector sz
defines size(C)
. For example, cell([2 3])
returns a 2-by-3 cell array.
D = cell(obj)
converts a Java array, .NET System.String
or System.Object
array, or Python sequence into a MATLAB cell array.
Input Arguments
expand all
n
— Size of square cell array
integer value
Size of a square cell array, specified as an integer value.
If
n
is0
, thenC
is an empty cell array.If
n
is negative, then it is treated as0
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz1,...,szN
— Sizes of dimensions
integer values
Sizes of the dimensions of the cell array, specified as integer values.
If the size of any dimension is
0
, thenC
is an empty cell array.If the size of any dimension is negative, then it is treated as
0
.Beyond the second dimension,
cell
ignores trailing dimensions with a size of 1. For example,cell(3,1,1,1)
produces a 3-by-1 cell array of empty matrices.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz
— Size
row vector of integers
Size, specified as a row vector of integers. Each element of sz
indicates the size of the corresponding dimension.
If the size of any dimension is
0
, thenC
is an empty cell array.If the size of any dimension is negative, then it is treated as
0
.Beyond the second dimension,
cell
ignores trailing dimensions with a size of 1. For example,cell([3 1 1 1])
produces a 3-by-1 cell array of empty matrices.
Example: sz = [2 3 4]
creates a 2-by-3-by-4 cell array of empty matrices.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
obj
— Input array
Java array or object | .NET array of type System.String
or System.Object
| Python sequence type
Input array, specified as:
Java array or object
.NET array of type
System.String
orSystem.Object
Python sequence type
Output Arguments
expand all
C
— Output array
cell array
Output array, returned as a cell array. Each cell contains an empty, 0-by-0 array of type double
.
D
— Converted array
cell array
Converted array, returned as a cell array.
Each cell contains a MATLAB object that has a type closest to the corresponding Java, .NET, or Python type. For more information, see:
Handle Data Returned from Java Methods
Automatically Convert Python Types to MATLAB Types
.NET Type to MATLAB Type Mapping
Examples
collapse all
Create Cell Array
Open Live Script
When related pieces of data have different data types, you can keep them together in a cell array. Each cell contains a piece of data. To refer to elements of a cell array, use array indexing. You can index into a cell array using smooth parentheses, ()
, and into the contents of cells using curly braces, {}
.
Create a cell array that contains several temperature readings taken on a given date. Specify a date as a character vector, and temperatures as an array of doubles. To store these pieces of data in a cell array, enclose them in curly braces.
C = {'2017-08-16',[56 67 78]}
C=1×2 cell array {'2017-08-16'} {[56 67 78]}
Add readings for different dates to the cell array. One way to add more cells is to expand the cell array by assignment, just as you can expand an ordinary array.
C(2,:) = {'2017-08-17',[58 69 79]};C(3,:) = {'2017-08-18',[60 68 81]}
C=3×2 cell array {'2017-08-16'} {[56 67 78]} {'2017-08-17'} {[58 69 79]} {'2017-08-18'} {[60 68 81]}
Index into the first row of C
. When you index with smooth parentheses, ()
, the result is a cell array that is a subset of the cell array.
C(1,:)
ans=1×2 cell array {'2017-08-16'} {[56 67 78]}
Index into the contents of a cell. When you index with curly braces, {}
, the result is the piece of data that is contained in the specified cell.
C{1,2}
ans = 1×3 56 67 78
Square Cell Array
Open Live Script
Create a 3-by-3 cell array of empty matrices.
C = cell(3)
C=3×3 cell array {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
3-D Cell Array
Open Live Script
Create a 3-by-4-by-2 cell array of empty matrices.
C = cell(3,4,2);size(C)
ans = 1×3 3 4 2
Clone Size from Existing Array
Open Live Script
Create a cell array of empty matrices that is the same size as an existing array.
A = [7 9; 2 1; 8 3];sz = size(A);C = cell(sz)
C=3×2 cell array {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
It is a common pattern to combine the previous two lines of code into a single line.
C = cell(size(A));
Tips
Creating a cell array of empty matrices with the
cell
function is equivalent to assigning an empty matrix to the last index of a new cell array. For example, these two statements are equivalent:C = cell(3,4,2);C{3,4,2} = [];
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
See Cell Array Limitations for Code Generation (MATLAB Coder).
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
See distributed.cell (Parallel Computing Toolbox).
Version History
Introduced before R2006a
expand all
R2023b: Partial contents as well as size and data type of arrays in cells are displayed
Between R2021b and R2023b, MATLAB displayed the entire contents of an array in a cell if the contents fit in the display. If the array did not fit, MATLAB showed as much of the array as the display allowed.
Starting in R2023b, if the entire contents of an array in a cell do not fit in the display, MATLAB shows as much of the array as the display allows, as well as the size and data type of the array. For example, as of R2023b, MATLAB displays partial contents as well as size and data type for a cell that contains an array of 100 double values.
D = {3.14,[1:100]};D(2)
ans = 1×1 cell array {[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 … ] (1×100 double)}
If the first element of the array is itself too large to display, such as an enumeration member with a long name, MATLAB displays only the size and data type of the array.
The disp
function follows the same behavior.
R2021b: Partial contents of arrays in cells are displayed instead of size and data type
Before R2021b, MATLAB displayed the entire contents of an array in a cell if the contents fit in the display. If the array did not fit, MATLAB displayed the size and data type of the array.
Starting in R2021b, if the entire contents of an array do not fit in the display, MATLAB shows as much of the array as space allows instead of the size and data type. For example, as of R2021b, MATLAB displays partial contents of a cell that contains an array of 100 double values.
D = {3.14,[1:100]};D(2)
ans = 1×1 cell array {[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 … ]}
If the first element of the array is itself too large to display, such as an enumeration member with a long name, MATLAB displays only the size and data type.
The disp
function follows the same behavior.
R2019a: Cell array expansion is consistent with general array expansion
Starting in R2019a, the dimensions of an expanded cell array are consistent whether you use curly braces or parentheses for indices. Previously, the output dimensions were different when you did not specify indices for all dimensions. Indexing with curly braces now matches the previous behavior for indexing with parentheses, which is consistent with general array expansion. For example:
If
C
is 1-by-2-by-3-by-4, thenC(5,2)={3}
andC{5,2}=3
both expandC
to 5-by-2-by-3-by-4. Previously,C{5,2}=3
resulted in a 5-by-24 array.If
C
is 0-by-5 or 5-by-0, thenC(3)={2}
andC{3}=2
expandC
to 1-by-3. Previously,C{3}=2
resulted in a 1-by-5 array whenC
was 0-by-5 and in a 3-by-1 array whenC
was 5-by-0.If
C
is 0-by-2-by-3, thenC(3)={2}
andC{3}=2
produce errors because it is ambiguous which dimension to expand. Previously,C{3}=2
resulted in a 1-by-6 array.
See Also
num2cell | table2cell | iscell | cell2mat | cell2struct | cell2table | mat2cell | struct2cell
Topics
- Create Cell Array
- Preallocate Memory for Cell Array
- Access Data in Cell Array
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB:
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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: .
You can also select a web site from the following list:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office