# Deleting an element from a cell array 28

Posted by **Doug Hull**,

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.

**Category:**- Format: Video,
- Level: Basic

## 28 CommentsOldest to Newest

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

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

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

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

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

**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-2ans =

4 -2

5 -2ans =

Empty matrix: 0-by-2ans =

8 -2

9 -2

10 -2

>>

**7**of 28

Got it ;)

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

**8**of 28

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

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

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

**11**of 28

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

**12**of 28

Thanks, this was really helpful!!!

**13**of 28

Thank you very much Dough exactly what I needed!

**14**of 28

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

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

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

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

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

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

**20**of 28

@Daniel,

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

Doug

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

**22**of 28

>> b = cell(2,2) b = [] [] [] [] >> b{1,1} = 'a' b = 'a' [] [] [] >> b{1,1} = [] b = [] [] [] []

Note the use of curley braces on my version.

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

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

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

**26**of 28

There is a function which specifically removes indicated row/columns from a cell, called fun_removecellrowcols

http://www.mathworks.com/matlabcentral/fileexchange/46196-fun-removecellrowcols

**27**of 28

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’

**28**of 28

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?

## Recent Comments