Stuart’s MATLAB Videos

Watch and Learn

Deleting an element from a cell array 28

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.

28 CommentsOldest to Newest

matt fig replied on : 1 of 28

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 28

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 28

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

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 28

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

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

Philip replied on : 8 of 28

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

Eran replied on : 9 of 28

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

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 28

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 28

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

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 28

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

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!

Dheny replied on : 21 of 28

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.

Dheny replied on : 23 of 28

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 28

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

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.

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’

Thank you, I’ve recently been searching for information about this topic for ages and yours is
the greatest I have found out so far. But, what
in regards to the bottom line? Are you certain in regards to the source?

Add A Comment

What is 5 + 9?

Preview: hide