Tags: array, cell, cellarray, cells, documentation, function, matlab, programming, removing

Removing cells from a cell array

On Programmer » Matlab

2,246 words with 3 Comments; publish: Tue, 06 May 2008 17:01:00 GMT; (200125.00, « »)

I don't think there's a function for removing a cell from a cell

array in Matlab. I've looked in all the Matlab documentation on and

offline that I could find, but nothing mentions this.

Let's say I have a cell array:

a{1} = 'hello';

a{2} = 'world';

a{3} = 'bye';

a{4] = 'now';

If I want to delete a{3}:

a{3} = [];

But that doesn't actually remove the 3rd cell and shunt everything

after it along, as I would like. This is a problem for me, because I

want to use the length of the cell array to know how many items there

are.

I could set up a proxy variable like nElements and increment and

decrement that as necessary, or something similar. But I'd really

much prefer to actually remove the cell.

Many thanks if anyone can help,

Greg

All Comments

Leave a comment...

  • 3 Comments
    • Greg Detre:

      <SNIP wants to remove a cell for good...

      one of the solutions

      a{1}='hello';

      a{2}='world';

      a{3}='bye';

      a{4}='now';

      % cells to keep

      ix=[1 2 4];

      a=a(ix)

      % it'll be your (new) task to find a way to

      % determine <ix>...

      us

      #1; Tue, 06 May 2008 17:02:00 GMT
    • That's ingenious, neat and (now seems) obvious in equal measure.

      Thank you very much indeed.

      us wrote:

      >

      > Greg Detre:

      > <SNIP wants to remove a cell for good...

      > one of the solutions

      > a{1}='hello';

      > a{2}='world';

      > a{3}='bye';

      > a{4}='now';

      > % cells to keep

      > ix=[1 2 4];

      > a=a(ix)

      > % it'll be your (new) task to find a way to

      > % determine <ix>...

      > us

      #2; Tue, 06 May 2008 17:03:00 GMT
    • Greg Detre wrote:

      > If I want to delete a{3}:

      > a{3} = [];

      > But that doesn't actually remove the 3rd cell

      Hi,

      try this :

      a(3)=[]

      read also this :

      <[url]http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/ch_da38a.html[/

      url]>

      See difference between cell indexing and content indexing

      Jrme

      #3; Tue, 06 May 2008 17:04:00 GMT