Updated March 4, 2023
Introduction to Matlab Cell
Cell() is an inbuilt function in Matlab which is used to construct a cell array which is a data type having data containers being indexed. Those data containers are called as cells. Each cell in a cell array can have data of different type. Texts can be contained in cell arrays in various formats such as lists containing the texts, arrays of different sizes containing numerical values or arrays having combinations of both text and numbers. Sets of cells can be referred with smooth parentheses indices enclosed – (). The contents of the cells are accessed with curly braces {} being indexed.
Syntax of Matlab Cell
Given below are the syntax of Matlab Cell:
Syntax | Description |
CA = cell(n) | The syntax is used to return a cell array CA, of size n-by-n creating matrices with empty value. |
CA = cell(sz1,…,szN) | The syntax is used to return a cell array of size sz1-by-sz2-by-….-szN creating matrices with empty value where Szi is the indication for the size of ith dimension out of N dimensions. |
CA = cell(sz) | The syntax is used to return a cell array of size ‘sz’, CA, creating matrices with empty value. |
Conv = cell(obj) | This syntax is used to convert .NET System.String, System. Object array, a Java array or Python sequence to that of an object of type, Matlab cell array. |
A cell array can be used to keep related data points having different data types, in single cell array together. One piece of data is contained in one single cell. In order to refer elements in a cell array, array indexing can be used. Array indexing is applied to index a cell array using() (smooth parentheses) and the contents of cells can be mentioned using curly braces {}.
Input Arguments:
- Size of a cell array – ‘n’: Size of any square type cell array can be specified with value of integer type. Positive integer value defines the actual size of the cell array whereas n with value 0, gives an empty cell array. In case of n with negative value, is treated by the function as 0.
- dimensions’size (szi): Size of any dimension of a cell array can be specified with value of integer type. Positive integer value defines the actual size of the ith dimension of the cell array whereas szi with value 0, gives cell array without having elements in the corresponding ith dimension. In case of szi with negative value, is treated by the function as 0. Trailing dimensions having size of value 1, are ignored by the cell array for any dimension beyond the second dimension.
- Size(sz): Size of any dimension of a cell array can be specified in the form of a row vector of integers indicating sizes of dimensions corresponding to the array indexes. Positive integer value defines the actual size of the ith dimension of the cell array whereas sz with value 0, gives cell array having empty value. In case of sz with negative value, is treated by the function as 0. Trailing dimensions having size of value 1, are ignored by the cell array for any dimension beyond the second dimension.
- Input array (obj): Input arrays are the objects which are fed to the function to be converted to cell array object. The input array can be an array of Java type, .NET, System.Object, Python sequence type or System.String.
- Output array(CA): Output arrays are the return value of the function resulting in type of cell array having a, 0-by-0 array (empty) of type double in each cell.
- D — Converted array: Converted array are the return value of the function resulting in type of cell array. Each cell gets occupied with any Matlab object which supports a type that is closest to the corresponding input object type.
Examples of Matlab Cell
Given below are the examples mentioned :
Example #1
The below command creates a square cell array of size 5×5 with the implementation of cell() function.
Code:
Cell_Arr = cell(5)
Output:
Example #2
The below command creates a 3 dimensional cell array having 2x3x4 size for first, second and third dimension respectively, with implementation of cell() function.
Code:
Cell_Arr = cell(2,3,4);
size(Cell_Arr)
Output:
Example #3
The below example is written to create an empty cell array having the size same as given input array.
Code:
M = [7 9; 2 1; 8 3];
sz = size(M)
Cell_Arr = cell(size(M))
Output:
The output results in the cell array having cells arranges as:
The resultant cell array Cell_Arr is an empty cell array having size equals to non-empty input array M.
It is also known as cloning size of an existing array.
Additional Note:
- Using Cell() method to create a cell array of empty matrices is as same as assignment of an empty matrix to any new cell array as the last index.
Example:
The below two commands produce the same behavior:
CA = cell(2,3,2);
CA{2,3,2} = [];
- If during the execution of the program, data needs to be inserted into a cell array, the cell array can be created at first, using {} as cell array construction operator.
Example:
CA = {1,3,2;
‘try’,rand(4,9,3),{11; 22; 33}}
CA=3×2 cell array
{[ 1]} {[ 2]} {[ 3]}
{‘try’} {4x9x3 double} {3×1 cell}
- The curly braces, {}, is also be used to create an empty valued i.e. 0-by-0 cell array.
- A cell can be used for preallocation of a cell array for future data assignment operations. Cell() method also extends its feature to convert certain types of objects such as Java, .NET, or Python data structures to that of cell arrays containing Matlab objects closest to the given data type.
Recommended Articles
This is a guide to Matlab Cell. Here we discuss the introduction to the Matlab Cell along with the examples respectively. You may also have a look at the following articles to learn more –