Search code examples
stringmatlabdelete-rowcell-array

Erase rows from a cell array depending on empty cells in each row


I have a cell array with 8 columns. Sometimes, in the last 5 columns I obtain this: [ ] (empty cell). In case it happens I want to completely erase that row. For instance:

19970101 18659 183 '19980820' '00018659' 'RUNYON L' '00001534' 'MERRILL'
19970101 18290 183 '19981221' '00018290' 'MANTON S' '00001534' 'MERRILL'
19970101 835   183    [ ]        [ ]        [ ]        [ ]        [ ]
19970101 10280 183 '19980819' '00010280' 'BRENNAN S' '00001534' 'MERRILL'

What I would like to get:

19970101 18659 183 '19980820' '00018659' 'RUNYON L' '00001534' 'MERRILL'
19970101 18290 183 '19981221' '00018290' 'MANTON S' '00001534' 'MERRILL'
19970101 10280 183 '19980819' '00010280' 'BRENNAN S' '00001534' 'MERRILL'

Thank you very much for your help.


Solution

  • Assuming input_cellarray to be the input cell array, try this -

    input_cellarray(all(cellfun(@numel,input_cellarray),2),:)
    

    If you want that we must remove rows where exactly the last 5 columns have empty cells [], then you need to tweak the code a bit -

    input_cellarray(any(cellfun(@numel,input_cellarray(:,end-4:end)),2),:)