Stuart’s MATLAB Videos

Watch and Learn

Deleting an element from a cell array 27

Posted by Doug Hull,

I recently had a situation where I needed to delete an entry from a cell array. When I tried A{3} = [ ], I did not delete the third element, I actual just set it to [ ].

What Loren helped me realize is that cell arrays use () just like regular MATLAB arrays to index into elements of the array, while preserving type of the container. i.e., index into a numeric array with () and you’ll get a numeric array back. Index into a cell array with () and you’ll get a cell array back. {} represents an additional syntax to access the underlying data.

27 CommentsOldest to Newest

matt fig replied on : 1 of 27

It is worthwhile to note that while

A(2) = []

works for a cell array,

A(2) = [5 6 7]

does not (it errors). This seems to be a source of confusion that I often see in those learning to work with MATLAB cell arrays. The correct syntax, of course, is

A(2) = {[5 6 7]}


A{2} = [5 6 7]

Andrew replied on : 2 of 27

Thanks for this Doug.

I was actually toiling with this just last night. My solution was pretty inelegant, in that I simply left the null element there and implemented a conditional continue statement inside my for loop (as I analyzed each element) to execute if it was empty, to go to analyzing the next non-null element.

This will make my code much nicer.

dhull replied on : 3 of 27


Thank you for posting that comment. When I post little tricks like this, I sometimes worry that they are too specialized or don’t have enough information in them. Good to hear this found you at the right time.

Not all of us can call Loren up when we need help, so I thought I would pass this trick on!


olga replied on : 4 of 27

I am trying to have a code to delete REPEAT from an cell array “a”, and the always run out array (the index exceeds, even though the loop is only up to length(a)
. can you help.

here is the code:


a={‘A’ ‘A’ ‘B’ ‘B’ ‘B’}

for k=1:length(a)
for m=k+1:length(a)

if strcmp(a{k},a{m})

dhull replied on : 5 of 27


This is the tricky thing when deleting things out of a vector in a loop. Every time you delete one, the vector gets shorter, and you miss something!

How about you count from the back to the front?


Stephan replied on : 6 of 27

Hey Doug,

thanks for this. It is really helpful.

I am wondering if there is also a possibility to delete empty cells, where you don’t know where exactly the empty bins are.

In the following example I wpould like to delete all empty cells. In this case this means cell 1 and 3.


ans =
Empty matrix: 0-by-2

ans =
4 -2
5 -2

ans =
Empty matrix: 0-by-2

ans =
8 -2
9 -2
10 -2

Philip replied on : 8 of 27

Wow! This really helped me out a lot. Thank you. I was getting frustrated with all those empty cells!

Eran replied on : 9 of 27


for deleting in for loop, the best solution that I can think of is this:

l = {‘a’,’b’,’c’,’d’};
to_delete = false(size(l));
for i=1:length(l)
if (condition)
to_delete(i) = true;
l(to_delete) = [];

daniel replied on : 10 of 27

Two awesome tricks, both the one about emptying cell arrays (which should be in the matlab documentation, I think), and the one for elliminating empty cells. Thanks!

Nasir replied on : 15 of 27

i am trying to delete zeros from an array a=[1 0 2 1 0 1 2]. . the code seems like

a=[1 0 2 1 0 1 2];

for i=1:length(a);

if a(i)>0;

after removing zeros the elements in array remains 5 but i returns 6. any solution for that ?

doug replied on : 16 of 27


Take the a(a==0)=[];

Out of the loop. Put is before. The length of a is changing inside the loop, which is trying to loop through the number of times of the original length of a.


Filip replied on : 17 of 27

I have an cell array 1×4.
keys = [1×13 double 1×13 double 1×13 double 1×13 double].
I want to delete this element: keys{1,4}(1,8)
When I use keys{1,4}(1,8) = []
matlab says:??? A null assignment can have only one non-colon index.

Can you please help me with this problem?

doug replied on : 18 of 27


You will need to pull the value of the cell array out, then delete it, and put it back in the cell. You can not do this in one operation.


Daniel replied on : 19 of 27

how can i remove an element from an array in a cell?

For example:

i’ve a 16×7 cell. In each element of this cell, i have a vector (lenght = 6). I need to remove the 4º element of this vector.

How can i do it??

Thanks all!

Doug replied on : 20 of 27


You will need to pull the vector out, delete the value from the vector and then put it back in.


Dheny replied on : 21 of 27

I’ve a cell (16,50) and i need to remove some elements, but matlab shows me: A null assignment can have only one non-colon index.

for example:

b = cell(16,50)

b(1,1) = []; its not possible.

How can i do it?

Doug replied on : 22 of 27
>> b = cell(2,2)

b = 

    []    []
    []    []

>> b{1,1} = 'a'

b = 

    'a'    []
     []    []

>> b{1,1} = []

b = 

    []    []
    []    []

Note the use of curley braces on my version.

Dheny replied on : 23 of 27

Doug, i need something like that:

b = cell(2,2)

b =
[1 2 3] [4 5 6] []
[0 0 0] [7 8 9] []

then, i remove b{1,1}

b =
[4 5 6] []
[0 0 0] [7 8 9] []

its possible?

Doug replied on : 24 of 27


It does not make sense in MATLAB to have a cell array where one row has two columns and the second row has three columns. Arrays are rectangular.


Dheny replied on : 25 of 27


Thanks, man. I found a way to do what i need. i’ll use a cell into a cell. Like that:
b = cell(3,1)

b{1,1} = {};

In this way, i can do what i need.

José replied on : 27 of 27

Assign the null value [] making reference to the position of the cell array element (between parenthesis () ) instead of its value (between keys {} ). This will delete such element and shrink the cell array conveniently.

You may also delete an entire row or column when the cell is a matrix (you may extrapolate this to an N-dimensional cell).

Example with a cell vector:

>> CellArray = {‘a’, ‘b’, ‘c’}
CellArray =
‘a’ ‘b’ ‘c’
>> CellArray (2) = []
CellArray =
‘a’ ‘c’

Example with a cell matrix:
>> CellMatrix = {‘a’, ‘b’, ‘c’; ‘d’, ‘e’, ‘f’}
CellMatrix =
‘a’ ‘b’ ‘c’
‘d’ ‘e’ ‘f’

>> CellMatrix(:,2) = []
CellMatrix =
‘a’ ‘c’
‘d’ ‘f’

Add A Comment

Your email address will not be published. Required fields are marked *

What is 3 + 10 ?

Preview: hide