MATLAB: How to delete rows of empty cells from a cell array

cell arraydelete rows

Is there a way I can delete empty cell arrays and preserve the order of the cell?
In the code I provided, it would be very easy to hardcode, but is there a way of doing it where you don't know where the 1st and last empty value is? The code I provided is just a simplified version of what I am trying to do.
% Create cell array
A = cell{4,3};
% Fill first 2 rows of cell
for k = 1:2
A{k,1} = 1;
A{k,2} = 2;
A{k,3} = 3;
A{k,4} = 4;
end
% Delete the 2 empty rows
% Desired output
%{
A =
2×4 cell array
{[ 1]} {[ 2]} {[ 3]} {[ 4]}
{[ 1]} {[ 2]} {[ 3]} {[ 4]}
%}

Best Answer

A = cell(4,3);
% Fill first 2 rows of cell
for k = 1:2
A{k,1} = 1;
A{k,2} = 2;
A{k,3} = 3;
A{k,4} = 4;
end
A(all(cellfun(@isempty, A),2),:) = []
A = 2x4 cell array
{[1]} {[2]} {[3]} {[4]} {[1]} {[2]} {[3]} {[4]}
Related Question