Cell arrays in Matlab
Let me tell you about using cell arrays. In this lesson, I'll show you how to work with them.
What exactly is a cell array? Cell arrays are arrays in which each element, called a "cell," stores another array inside. Essentially, it's an array of arrays. Within the array, the elements must be of the same type. However, cells can also contain arrays with different types of data.
How to create an array of cells?
To create an array of cells, use the function cell(m,n), where m and n are the number of rows and columns of the cell array.
For example, type cell(3,3)
>> myVar=cell(3,3)
myVar =
3×3 cell array
{0×0 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double}
The function cell(3,3) creates a 3x3 array of cells with 3 rows and 3 columns and assigns it to the variable myVar. Initially, all cells are empty.
Alternatively, you can also define the cell array by indicating the cell data inside curly braces.
A={"Math", "Latin", "Science"; 27, 30, 18}
A =
2×3 cell array
{["Math"]} {["Latin"]} {["Science"]}
{[ 27]} {[ 30]} {[ 18]}
How to insert arrays into an array of cells?
To assign an array to a cell, write the name of the cell array and indicate in curly braces the row and column number of the cell.
Then write the array you want to insert into the cell after the equals sign (=).
For example, type myVar(1,1)={ [1 2;3 4] } to assign the array [1 2; 3 4] to cell (1,1):
>> myVar(1,1)={[1 2;3 4]}
myVar =
3×3 cell array
{2×2 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double}
Now type myVar(1,3)={ ['A' 'B' 'C' 'D'] } to assign the array ['A' 'B' 'C'] to cell (1,3):
>> myVar(1,3)={['A' 'B' 'C' 'D']}
myVar =
3×3 cell array
{2×2 double} {0×0 double} {'ABCD' }
{0×0 double} {0×0 double} {0×0 double}
{0×0 double} {0×0 double} {0×0 double}
The cell array is composed of two arrays. The data type of the first array is numeric, while that of the second array is alphanumeric.
Note. It is not necessary to fill all cells or to follow a filling order. The other cells without arrays remain empty.
How to access cells?
To read the contents of a cell, type the name of the cell array, indicating the row and column number of the cell in parentheses.
For example, type myVar(1,3) to read the contents of cell (1,3).
>> myVar(1,3)
ans =
1×1 cell array
{'ABCD'}
In the case of cell arrays, parentheses are used for indices and square brackets for contents.
You can also use slicing to select multiple cells at once.
For example, type myVar(1:2,1:2) to select the cells within the first two rows and columns of the cell array.
>> myVar(1:2,1:2)
ans =
2×2 cell array
{2×2 double} {0×0 double}
{0×0 double} {0×0 double}
This way, you can extract multiple pieces of data from the cell array with a single selection.
How to display cell arrays?
To see all the contents of a cell array, use the command celldisp()
For example, type celldisp(myVar) to display the contents of the cell array stored in the variable myVar.
>> celldisp(myVar)
myVar{1,1} =
1 2
3 4
myVar{2,1} = [ ]
myVar{3,1} = [ ]
myVar{1,2} = [ ]
myVar{2,2} = [ ]
myVar{3,2} = [ ]
myVar{1,3} = ABCD
myVar{2,3} = [ ]
myVar{3,3} = [ ]
You can also display the contents graphically using the function cellplot()
For example, type cellplot(myVar)
>> cellplot(myVar)
This function displays the contents of the cell array using a graphical layout.
At this point, you should know what cell arrays are and how to use them in Matlab.