Matlab

How to Create Cell Arrays in MATLAB

A cell array is a type of array that contains data having different data types and data sizes. The indexed data containers in a cell array are called cells. These arrays can contain, texts, numbers, the combination of text and numeric data, or numeric data of different sizes. We can easily create cell arrays in MATLAB and use these arrays to store data from a variety of variable resources.

How to Create Cell Arrays in MATLAB?

MATLAB provides us with two methods to create a cell array as given below:

1: How to Create Cell Array Using the {} Operator in MATLAB

The most common and widely used method of creating a cell array in MATLAB is using the {} operator. This operator is called a cell array construction operator that allows us to put data into the cell array.

Example 1: How to Create a Cell Array Having Data Using {} Operator in MATLAB?

In this MATLAB code, we use the {} operator to create a cell array having some data in MATLAB.

A = {'K','Z', 'H', 'A'; 21 15 20 0}

 

Example 2: How to Create an Empty Cell Array Using {} Operator in MATLAB?

The given example creates an empty cell array using the {} operator in MATLAB.

A = {}

 

2: How to Create a Cell Array Using the cell() Function in MATLAB?

We can also create a cell array in MATLAB using the cell() function. This function enables us to create a cell array having the specified size as an input argument and returns a cell array corresponding to the given size.

Syntax

The cell() function uses different syntaxes to create a cell array in MATLAB, which are given below.

C = cell(n)
C = cell(sz1,...,szN)
C = cell(sz)

 
Here:

The function C = cell(n) creates an n-by-n empty cell array.

The function C = cell(sz1,…,szN) creates an empty cell array having size sz1-by-sz2-by…by-szN where, sz1,sz2,…,szN indicates each dimension of the created cell array.

The function C = cell(sz) yields an empty cell array having size sz where sz represents a vector defining the size of the created cell array.

Example 1: How to Create an Empty Cell Array Using the cell(n) Function in MATLAB?

In this MATLAB code, we use the cell(n) function to create a cell array having size n=5 in MATLAB.

C = cell(5)

 

Example 2: How to Create an Empty Cell Array Using the cell(sz1,sz2,…,szN) Function in MATLAB?

The given example creates an empty cell array using the cell(3,7,3) function in MATLAB.

C = cell(3,7,3)

 

Conclusion

A cell array is a type of array that contains data having different types and sizes. These arrays include numeric data, texts, and a combination of numeric data and strings. In MATLAB, these arrays can be created using the {} operator and the cell() function. The guide has covered both these methods with examples that help us find the best method according to our needs.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.