Updated June 27, 2023

## 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. Various types of functions and operations 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 text, lists, numbers arrays of different sizes. The main advantage of using the cell array is storing the elements of different data types and sizes.

### How Cell Array Works in Matlab?

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

**Y=cell(x):**This returns an 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 0, it results in an empty cell array; if the size is given as any negative value, it is considered 0. The accepted data types are single, double, int8, int32, int16, int64, uint8, uint16, uint32and uint64. If we have information on different data types and they have different sizes, then we can use a cell array.

Array Indexing refers 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 require contagious memory allocation. If we gradually increase the number of cells or several elements in an array, then it will be a vast array, and while storing it, we will get an ‘Out of Memory’ error.

### Examples of Matlab Cell Array

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

#### Example #1

To understand the storage of elements in the cell array.

**Code:**

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

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

X

**Output:**

#### Example #2

To create an empty cell array that is 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:**

We can access the elements present in the array in two ways. Please find them below:

- We can access the values by mentioning the indices of the elements using a () bracket.
- Using the {} bracket, we can access the values by specifying the indices of the elements. This notation helps identify the data within its individual cells.

#### Example #4

Access the elements 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 whether the array declared is a cell array or not by using Matlab’s iscell () function. It returns a logical value of 1 or 0 depending on the array type in the input argument. If the array is a cell array, it returns logical 1 (True); if not, 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’s see below conversions that can be done in Matlab to convert any type into the cell array:

- If the cells contain subarrays, we can convert them into cell arrays using Matlab’s “mat2cell” function. It divides the input arrays into the smaller parts of the array and then converts it into a cell array, and the resultant array can contain elements of different sizes.
- You can convert an array into a cell array using Matlab’s “num2cell” function, especially if the cell sizes are consistent throughout the array. The function splits the elements in the input array based on the specified dimension mentioned in the input argument. The syntax allows the dimension argument to be either a scalar or a combination of integers, indicating the dimensions to include 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, which can be of any type, can be multi-dimensional. The accepted data types for the input array include single, double, int8, int32, int16, int64, uint8, uint16, uint32, uint64, logical, char, string, categorical, datetime, cell, duration, and more. 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 a cell array using Matlab’s “struct2cell” function. 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, we can use the “fieldnames” function.

### Conclusion

Users widely utilize cell arrays in Matlab because they allow the storage of elements belonging to various data types and having different dimensions. Many business requirements demand the use of a cell array in this application, and you can achieve this by converting any object to a 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, examples, and code implementation. You may also look at the following articles to learn more-