Home > MATLAB > Cell arrays in MATLAB

Cell arrays in MATLAB

Cell arrays are used frequently in MATLAB code and they can be very useful in some situations, when you have data in both numeric and character formats and you want them to be together, for example. However, they can be tricky and sometimes not so obvious. While fiddling around with cell arrays, I came across some interesting points and thought it would be useful to other people as well.

Let’s create a cell array. I will leave out the ‘;’ so that we can see what is happening :

(1) test = cell(2,2)
test =
[] []
[] []

There are 2 ways (maybe more,  I don’t know) to assign data to a cell in a cell array :

(2) test{1,1} = 'KLM'
test =
'KLM' []
[] []

The other way to assign data to a cell would be :

(3) test(2,2) = {'Lufthansa'}
test =
'KLM' []
[] 'Lufthansa'

It looks like before you can assign something to a cell in a cell array, it has to be converted to a ‘cell object’ so that the receiving cell knows how to read what you send it. It can be done in either of the two ways above. When we assign ‘KLM’, we say test{}, which then makes sure that the string ‘KLM’ is converted to a cell object and inserted in the cell. In (3), we use the usual ( ) to point to the cell (2,2) in the ‘test’ cell array, but we enclose the string ‘Lufthansa’ in { } to make sure it goes in as a cell object.

To clarify, let’s look at what happens if you don’t do this. Let’s say :

(4) test(2,1) = 'Air France'
??? Conversion to cell from char is not possible.

What MATLAB says is that ‘Air France’ is a character array and it needs to be converted to something that the receiving cell of a cell array can understand.

From now, we’ll just stick to using { } to put in and get data from cell arrays.

Next, Let’s try :

(5) test{2,1} = {'Air Canada'}
test =
'KLM'              []
{1x1 cell}    'Lufthansa'

The (2,1) cell of ‘test’ is now a cell itself. Why is that? When we write (2), we say – make the string KLM into a cell object and put it in the (2,1) cell. When we write (5), we mean create a cell object for the string Air Canada, then put this cell object to the (2,1) cell.

To make this more clear, Let’s say we want to access the K in KLM. We write

(6) test{1,1}(1)
ans =

However, to access the C in Air Canada, we need to say

(7) test{2,1}{1,1}(5)
ans =

Basically, till we get to the data in a cell in a cell array, we use the { } braces. Once we get to the data, it is the usual character array or number and we can use the ( ) braces to access them.  MATLAB has a useful cellplot function that gives us a picture of how a cell array looks.

(8) cellplot(test)

which gives us

All cells are enclosed in boxes. To get to the data, we need to go into the box we are interested in. For instance, to get to the K in KLM, we need to go into the (1,1) box, so we write test{1,1}. Once inside, all we see is a character array which are indexed as usual using ().

To access the C in Air Canada, we first say test{} to get in the (2,1) box. But we see one more box inside. So we say test{}{}. Now all we can see is a character array Air Canada which can be indexed in the usual way using ( ).

Finally, let’s say :

(9) test{1,2} = {'UPS'; 'Delta'}

This is equivalent to :

test{1,2}{1,1} = 'UPS';
test{1,2}{2,1} = 'Delta';

We would have the picture as :

To access, for instance, the P in UPS, we would follow the same steps as before.

Let me know what you think. Comments welcome.

Categories: MATLAB Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: