Doug's MATLAB Video Tutorials

Deleting an element from a cell array 25

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.

25 CommentsOldest to Newest

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]

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.

@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

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

@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

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

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

@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) = [];

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!

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 ?

@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

I have an cell array 1×4.
keys = [1x13 double 1x13 double 1x13 double 1x13 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?

@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

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!

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?

>> b = cell(2,2)

b = 

    []    []
    []    []

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

b = 

    'a'    []
     []    []

>> b{1,1} = []

b = 

    []    []
    []    []

Note the use of curley braces on my version.

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?

@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

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.

Add A Comment

What is 7 + 8?

Preview: hide

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