⇦ Back

A cell array is an array of data of any type. Take a look at the documentation here.

1 Creating

In general, a cell is created by using curly brackets:

% Create a 3x1 cell
c = {
    'A string',
    1,
    3.14159265
}
## c =
## {
##   [1,1] = A string
##   [2,1] = 1
##   [3,1] = 3.1416
## }

Alternatively, an empty cell can be declared with the cell() function:

% Create an empty 2x2 cell
c = cell(2, 2)
## c =
## {
##   [1,1] = [](0x0)
##   [2,1] = [](0x0)
##   [1,2] = [](0x0)
##   [2,2] = [](0x0)
## }

You can confirm that it is empty by using cellfun(@isempty, c). This is a function that returns 1 for each element that is empty:

% Create an empty 2x2 cell
c = cell(2, 2);

% Is this cell empty?
cellfun(@isempty, c)
## ans =
## 
##   1  1
##   1  1

Initialising a cell (creating a non-empty cell) using curly brackets will result in this function showing zeros:

% Create a 1x3 cell
c = {'A string', 1, 3.14159265};

% Is this cell empty?
cellfun(@isempty, c)
## ans =
## 
##   0  0  0

A cell can contain multi-dimensional objects…you can even have cells-of-cells!

% Create a 3x1 cell containing a string, a matrix and a cell
c = {
    'A string',
    rand(2, 2),
    {1, 2, 3}
}
## c =
## {
##   [1,1] = A string
##   [2,1] =
## 
##      0.3131   0.9608
##      0.1504   0.7186
## 
##   [3,1] =
##   {
##     [1,1] = 1
##     [1,2] = 2
##     [1,3] = 3
##   }
## 
## }

2 Indexing vs Subsetting

Use curly brackets to get the value at an index in a cell. Remember that Octave is a 1-indexed language so the first element is number 1:

% Create a 1x3 cell
c = {1, '2', 'three'};

% Index the cell
c{1}
## ans = 1

Use round brackets to create a subset of the cell that includes the elements at the index or indexes you’ve provided:

% Create a 1x3 cell
c = {1, '2', 'three'};

% Subset the cell
c(1)
## ans =
## {
##   [1,1] = 1
## }

3 Describing

Use size(), rows() and columns() to display details about your cell:

c = {
    'A string',
    rand(2, 2)
};

size(c)
rows(c)
columns(c)
## ans =
## 
##    2   1
## 
## ans = 2
## ans = 1

This tells us that our cell is 2x1 in size: 2 rows and 1 column.

4 Iterating Over

You can iterate over a cell using a for loop:

# Create a 1x3 cell
c = {'A', 'B', 'C'};

for i = 1:columns(c)
    % Index the cell
    c{:, i}
    % Subset the cell
    c(:, i)
endfor
## ans = A
## ans =
## {
##   [1,1] = A
## }
## 
## ans = B
## ans =
## {
##   [1,1] = B
## }
## 
## ans = C
## ans =
## {
##   [1,1] = C
## }

5 Concatenating

Provided your cells are the right shape, you can append one to the bottom or to the right of another in order to combine them:

c1 = {
    1, 2;
    3, 4, 
};
c2 = {
    10, 20;
    30, 40
};

% Append to the bottom
c3 = [
    c1;
    c2
]

% Append to the right
c3 = [c1, c2]
## c3 =
## {
##   [1,1] = 1
##   [2,1] = 3
##   [3,1] = 10
##   [4,1] = 30
##   [1,2] = 2
##   [2,2] = 4
##   [3,2] = 20
##   [4,2] = 40
## }
## 
## c3 =
## {
##   [1,1] = 1
##   [2,1] = 3
##   [1,2] = 2
##   [2,2] = 4
##   [1,3] = 10
##   [2,3] = 30
##   [1,4] = 20
##   [2,4] = 40
## }

⇦ Back