MATLAB Answers


How to delete entire row if any cell of the particular row is empty

Asked by Mekala balaji on 4 Mar 2016
Latest activity Commented on by madhan ravi
on 26 Mar 2019
Hi, I have the below cell array matrix. Some cells are empty, and I want to delete entire row if any cell is empty(Ex: 2, 3,4). And my final output should be only row1 & row5.
Index name1 name2 visit
1 MM_vrx_ty_98u prb05k 20
2 M_rx_ty_8u prbkv
3 prb05k 50
4 MM_vrx_ty_98u 11
5 GG_VL_ty_4u 9
Kindly help. Sincerely,


Sign in to comment.

2 Answers

Answer by Walter Roberson
on 4 Mar 2016
 Accepted Answer

row_has_empty = any(cellfun(@isempty, YourCell), 2); %find them
YourCell(row_has_empty,:) = []; %delete them

  1 Comment

The second row & 4th column is empty, but it is not deleted using the above code.
Kindly help.

Sign in to comment.

Answer by Pruthvi Raj Gampalwar on 26 Mar 2019

Input :
Matrix = [[{'a'};{'b'};{'c'};{''};{'b'};{'c'}],[{'a'};{'b'};{''};{'a'};{''};{'c'}],[{'a'};{''};{'c'};{'a'};{'b'};{'c'}]]
Matrix =
6×3 cell array
'a' 'a' 'a'
'b' 'b' ''
'c' '' 'c'
'' 'a' 'a'
'b' '' 'b'
'c' 'c' 'c'
Solution to delete complete Row if Empty cell is found
% Answer
Matrix(any(Empty_Matrix(:,[1,2,3]),2),:)=[] % deleting the entire row
Output :
Matrix =
2×3 cell array
'a' 'a' 'a'
'c' 'c' 'c'

  1 Comment

This answer was given before 2 years already.

Sign in to comment.