Search code examples
stringmatlabcell-array

Matlab remove empty cells from within cell array


I have a cell array that shows up like this

lists = 

    {240x1 cell}    {240x1 cell}

it contains letter strings, NOT numbers.

Only 200 of cells have actual content, so I need to truncate the cells getting rid of empty ones.

Now, lists(cellfun('isempty',lists)) = []; does not work, nor does it work with arrayfun. Changing 'isempty' to @isempty does not work either. However, if I separate each cell of lists to a cell, like this a = lists{1}, the cellfun procedure works.

Note, I am not looking for a solution for this specific case with 200 out 240 cells having content.

FWY, the lists is a subset of an array that textscan spits out. The problem is that some of the columns in the initial textscan output do have 240 content cells, so textscan just defaults to that length for all the columns.


Solution

  • You need to call cellfun twice, once on lists and once on its elements

    cellfun(@(x) x(~cellfun('isempty', x)), lists, 'uni', 0)