Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all empty cells in a cell array

I have an array of cells in which I want to select 3 lines so I used this temp = testresults(13:15,1:end).

The array being bigger, I get a lot of empty cells

{'Summary Test Re…'}    {'Overall'   }    {0×0 char          }    {'OVP Transition …'}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}
{'Pass/Fail'       }    {'Passed'    }    {'No Transition t…'}    {'Passed'          }    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}
{'Failed cases'    }    {'No failure'}    {0×0 char          }    {'No failure'      }    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}    {0×0 char}

Hence, I'm trying to remove the empty cells using temp(~cellfun('isempty',temp)) but, if all the empty cells are gone, it puts all my data in one column:

{'Summary Test Results'          }
{'Pass/Fail'                     }
{'Failed cases'                  }
{'Overall'                       }
{'Passed'                        }
{'No failure'                    }
{'No Transition time change'     }
{'OVP Transition level pass/fail'}
{'Passed'                        }
{'No failure'                    }

I've tried some variation of the function and I have also tried with cat(2, temp{:}) but I don't know how to keep the data in the right position.

How can I remove the empty cells without touching the position of the rest of the data?

like image 722
Jack Avatar asked Apr 28 '26 13:04

Jack


1 Answers

Given a cell array like this one:

temp = {
'Summary Test Re…', 'Overall',    '', '',                 'OVP Transition …', '', '', '', ''
'Pass/Fail',        'Passed',     '', 'No Transition t…', 'Passed',           '', '', '', ''
'Failed cases',     'No failure', '', '',                 'No failure',       '', '', '', ''
'',                 '',           '', '',                 '',                 '', '', '', ''};

we can find empty cells (as you already found out) using

empty = cellfun('isempty',temp);

Next, we can delete rows where all cells are empty with

temp(all(empty,2),:) = [];

and columns where all cells are empty with

temp(:,all(empty,1)) = [];

all(empty,1) returns a logical row vector where an element is true if all cells in that column are empty. We use this logical vector to index those array elements and set them to the empty array. Assigning an empty array is MATLAB speak for deleting the array element. Deleting complete rows and columns allows to preserve the array shape.

Note the difference between temp(:,1)=[] and temp{:,1}=[]. The first one deletes a column of array elements, the second one assigns an empty array to each of the cells in a column of the cell array.

like image 126
Cris Luengo Avatar answered May 01 '26 01:05

Cris Luengo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!