# Deleting an element from a cell array26

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.

matt fig replied on : 1 of 26

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]}

or

A{2} = [5 6 7]

Andrew replied on : 2 of 26

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 26

@Andrew,

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!

Thanks,
Doug

olga replied on : 4 of 26

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:

clc;clear

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

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

if strcmp(a{k},a{m})
a(m)=[]
m=m-1
end
end
end

dhull replied on : 5 of 26

@Olga,

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?

Doug

Stephan replied on : 6 of 26

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.

Cheers,
stephan

>>D{1:end}
ans =
Empty matrix: 0-by-2
ans =
4    -2
5    -2
ans =
Empty matrix: 0-by-2
ans =
8    -2
9    -2
10    -2
>>

Stephan replied on : 7 of 26

Got it ;)

D(cellfun(@(D) isempty(D),D))=[];

Philip replied on : 8 of 26

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

Eran replied on : 9 of 26

@Doug

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;
end
end
l(to_delete) = [];

daniel replied on : 10 of 26

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!

Benas replied on : 11 of 26

Thank you very much! I’ve been stuck with this problem for a while

Lani replied on : 12 of 26

Thanks, this was really helpful!!!

ali replied on : 13 of 26

Thank you very much Dough exactly what I needed!

Shamir Alavi replied on : 14 of 26

Thanks Doug, it was very useful. These little and simple things can really halt bigger works.

Nasir replied on : 15 of 26

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);

a(a==0)=[];
if a(i)>0;
flagp=1;
else
flagp=0;
end
end

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

doug replied on : 16 of 26

@Nasir

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.

-Doug

Filip replied on : 17 of 26

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.

doug replied on : 18 of 26

@Filip,

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.

Doug

Daniel replied on : 19 of 26

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 26

@Daniel,

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

Doug

Dheny replied on : 21 of 26

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 26
>> 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 26

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 26

@Dheny,

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.

doug

Dheny replied on : 25 of 26

Doug,

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.

What is 8 + 9?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.