How to delete rows of empty cells from a cell array

39 次查看(过去 30 天)
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]}
%}

采纳的回答

Walter Roberson
Walter Roberson 2021-2-24
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]}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by