## Introduction to Matlab Cell Array

Arrays are used to store the data which has any relevant information. In Matlab, arrays are stored in the form of rows and columns. There are various types of functions and operations that can be performed using the elements in an array. A cell array is the type of array in Matlab where the elements are put into respective cells. They can be in the form of text, list, numbers arrays which can be of different sizes. The main advantage of using the cell array is that we can store the elements of different data types and sizes.

### How Cell Array Works in Matlab?

In Matlab, cell arrays can be represented by using cell function. We can also just declare the type of array as a cell array and can assign the values to it afterward. Please find the below syntaxes that are used while working with cell array:

**Y=cell(x):**This returns array in the form of x by x dimension and empty matrix.**Y=cell(size):**This function returns an array of the given size mentioned in the input argument. For example, cell([3,4]) returns an array of 3 by 4 dimensions.**Y=cell(size1, size2…sizen):**This function returns an array of the given sizes mentioned in the input argument and each size indicates the size of each dimension present in it.**Y=cell(object):**This syntax converts any Java array, String or Object array, .Net system into a Matlab cell array.

The input arguments like size must be an integer value. If the size is given as 0, then it results in an empty cell array and if the size is given as any negative value then it is considered as 0. The data types that are accepted are single, double, int8, int32, int16, int64, uint8, uint16, uint32and uint64. If we have information of different data types and they have different sizes, then we can use cell array.

Array Indexing is used to refer to the different elements present in a cell array. They do not require any contagious memory locations to store the data present in the respective array. Each cell and the header present in the array requires contagious memory allocation. If we gradually increase the number of cells or number of elements in an array then it will be a very large array and while storing it, we will get ‘Out of Memory’ error.

### Examples of Matlab Cell Array

Let us see some examples of Matlab cell array which are as follows:

#### Example #1

To understand the storage of elements in the cell array.

4.5 (4,434 ratings)

View Course

**Code:**

`X{1,1} = [1 2 3; 0 4 7; 8 3 4];`

X{1,2} = 'Joseph Doe';

X

**Output:**

#### Example #2

To create cell array which is empty and has the same size as that of the existing array.

**Code:**

`X = [8 9; 4 1; 5 3];`

siz = size(X);

Y = cell(siz)

**Output:**

#### Example #3

**Code:**

`X = cell(3, 5);`

Y = {'Temp1', 'Temp2', 'Temp3', 'Temp4', 'Temp5'; 1 2 3 4 5}

**Output:**

There are two ways in which we can access the elements present in the array. Please find them below:

- We can access the values by mentioning the indices of the elements using () bracket.
- We can access the values by mentioning the indices of the elements using {} bracket which is used to identify the data in its individual cells.

#### Example #4

To access the elements, present in the cell array using the Array Indexing method.

**Code:**

`X = cell(3, 5);`

Y = {'Temp1', 'Temp2', 'Temp3', 'Temp4', 'Temp5'; 1 2 3 4 5;10,11,12,13,14}

Y(1:3,2:3)

**Output:**

We can determine if the array declared is a cell array or not by using iscell () function in Matlab. It returns a logical value that is 1 or 0 depending on the type of array in the input argument. If the array is a cell array, then it returns logical 1 (True) and if it is not then it returns logical 0(False). Please find the below example which explains the above concept:

#### Example #5

**Code:**

`X = cell(3, 5);`

Y = {'Temp1', 'Temp2', 'Temp3', 'Temp4', 'Temp5'; 1 2 3 4 5;10,11,12,13,14}

check=iscell(Y)

**Output:**

Let see below conversions that can be done in Matlab to convert any type into the cell array:

- If the cells contain subarrays, then we can convert them into cell array by using the “mat2cell” function in Matlab. It divides the input arrays into the smaller parts of the array and then converts it into cell array and the resultant array can contain the elements of different sizes.
- If the cell size is consistent in an array, then they can be converted into the cell array by using the “num2cell” function in Matlab. It splits the elements present in the input array and the dimension is decided as mentioned in the input argument. The dimension argument in the syntax can be scalar or a combination of integers which denotes the dimensions to be included in each cell. The dimension should be a positive integer and it should be in the range of 1 to the dimension of the input array.
- The input array can be multi-dimensional array which can be of any type and the data types that are accepted by the input array are single, double, int8, int32, int16, int64, uint8, uint16, uint32, uint64, logical, char, string, categorical, datetime, cell, duration, etc. The resultant array which is a cell array depends on the size and dimensions of the input array.
- We can also convert the structure to cell array by using the “struct2cell” function in Matlab. The elements present in the structure are copied to the resultant array which is a cell array but it will not contain the header names. If we want to display the header names, then we can use the “fieldnames” function.

### Conclusion

Cell arrays are widely used in Matlab as it can store the elements that belong to various data types and can have different dimensions. Due to this application, there are many business requirements that demand the use of cell array which can also be achieved by converting any object to cell array in Matlab.

### Recommended Articles

This is a guide to the Matlab Cell Array. Here we discuss the introduction and working of Matlab Cell Array along with example and code implementation. You may also look at the following articles to learn more-