How to delete rows of empty cells from a cell array

52 vues (au cours des 30 derniers jours)
David Rodriguez
David Rodriguez le 24 Fév 2021
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]}
%}

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Fév 2021
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]}
  2 commentaires
David Rodriguez
David Rodriguez le 24 Fév 2021
Thank you so much!
Pallavi Chaudhary
Pallavi Chaudhary le 15 Mar 2023
Thanks a lot :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by