Removing blank lines in a cell array
I have a cell array (10001x21)
where the first 3 columns have data in all rows. From Columns 4 to 21
I have data. Some cells of a certain row and some rows may be completely blank.
I want to keep only rows that have data in some columns from 4 to 21
, and remove all empty rows that have no data in columns (4:21)
. Any help would be appreciated in this case. Thank.
This is in Matlab environment
This is what the data looks like
'1Fb6' 2014 'F' [] [] [] [] [] [] [] [] [] []
'1Fc6' 2014 'F' [] [] [] [] [] [] [] [] [] []
'1Fd6' 2014 'F' [] [] [] [] [] 'ka1' [] [] [] []
'1Fk6' 2014 'F' [] [] [] [] [] [ ] [] [] [] []
'1Fy6' 2014 'F' [] [] [] [] [] [] [] [] [] []
'1Fz6' 2014 'F' [] [] 'na1' [] [] [] [] [] [] []
'1Fj6' 2014 'F' [] [] [] [] [] [] [] [] [] []
'1Fm6' 2014 'F' [] [] [] [] [] [] [] [] [] []
'1Fn6' 2014 'F' [] [] [] [] 'bo1' [] [] [] [] []
source to share
Suppose your cell array was called data
, with dimensions [rows, columns]
. You can only store all rows that have some data in 4: 21 columns in a new matrix named new_data
using this code:
new_data = data(~all(cellfun('isempty', data(:, 4:21)), 2), :);
To interrupt this:
-
cellfun('isempty', data(:, 4:21))
returns a size matrix[rows, columns-3]
. Any empty cells indata
from columns 4:21 are marked as 1, and all other cells are marked as 0. -
~all(cellfun(...), 2)
returns a vector of sizerows x 1
. It takes that previous matrix and sees if each row is an array of all 1 cells (in other words, if that row is completely empty). The functionall
returns true if the string is 1. However, due to the NOT (~) operator, all rows with only 1 are actually marked as 0, and all other rows are marked with 1. In other words, rows marked with 1 are strings, which we want to keep. -
new_data = data(all(...), :)
simply returns the data matrix, removing any rows that are completely empty at 4:21 columns.
source to share