Main Content

Add or Delete Cells in Cell Array

Cell arrays follow the same basic rules for expansion, concatenation, and deletion as other types of MATLAB® arrays. However, you can index into a cell array in two ways: with curly braces {} to access cell contents or with parentheses () to refer to the cells themselves. Keep this distinction in mind when you add, delete, or combine cells in a cell array.

Add Cells

A common way to expand a cell array is to concatenate cell arrays vertically or horizontally. Use the standard square bracket concatenation operator []. Separate elements with semicolons for vertical concatenation or commas for horizontal concatenation.

C1 = {'one',2};
C2 = {ones(3,3),'four'};

C = [C1; C2]
C=2×2 cell array
    {'one'     }    {[   2]}
    {3x3 double}    {'four'}

C_horz = [C1,C2]
C_horz=1×4 cell array
    {'one'}    {[2]}    {3x3 double}    {'four'}

Concatenating a cell array and a non-cell array encloses the non-cell array in a single cell. Therefore, the cell array must be a vector.

A = [1 2 3; 4 5 6];
C3 = [C1,A]
C3=1×3 cell array
    {'one'}    {[2]}    {2x3 double}

To create separate cells from the non-cell array, you can use num2cell.

C4 = [C,num2cell(A)]
C4=2×5 cell array
    {'one'     }    {[   2]}    {[1]}    {[2]}    {[3]}
    {3x3 double}    {'four'}    {[4]}    {[5]}    {[6]}

Cell arrays also support scalar expansion. That is, if you assign values to the contents of cells outside the existing array, the array expands to include them. The expanded array is rectangular, and any intervening cells contain empty numeric arrays. When assigning the contents of a cell, use curly braces.

C{3,3} = 9
C=3×3 cell array
    {'one'     }    {[       2]}    {0x0 double}
    {3x3 double}    {'four'    }    {0x0 double}
    {0x0 double}    {0x0 double}    {[       9]}

C{end,end+1} = []
C=3×4 cell array
    {'one'     }    {[       2]}    {0x0 double}    {0x0 double}
    {3x3 double}    {'four'    }    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}    {[       9]}    {0x0 double}

To replace the contents of cells, define a cell array using curly braces, and then assign it to an equivalently sized set of cells using parentheses.

C(3,:) = {'replacement', rand(2,2), 42, 'row'}
C=3×4 cell array
    {'one'        }    {[       2]}    {0x0 double}    {0x0 double}
    {3x3 double   }    {'four'    }    {0x0 double}    {0x0 double}
    {'replacement'}    {2x2 double}    {[      42]}    {'row'     }

Delete Cells

The syntax for removing rows or columns of a cell array is consistent with other MATLAB arrays. Set the cells equal to a pair of empty square brackets. For instance, remove the second row of C.

C(2,:) = []
C=2×4 cell array
    {'one'        }    {[       2]}    {0x0 double}    {0x0 double}
    {'replacement'}    {2x2 double}    {[      42]}    {'row'     }

Enclosing indices in curly braces replaces the contents of a cell with an empty array.

C{1,1} = []
C=2×4 cell array
    {0x0 double   }    {[       2]}    {0x0 double}    {0x0 double}
    {'replacement'}    {2x2 double}    {[      42]}    {'row'     }

Combine Cells

Cells can contain data of any type or size, so combining cells or extracting data from multiple cells at the same time requires that the data is compatible. For details and examples, see Access Data in Cell Array.

Related Topics