Azzera filtri
Azzera filtri

How to delete rows of empty cells from a cell array

45 visualizzazioni (ultimi 30 giorni)
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]}
%}

Risposta accettata

Walter Roberson
Walter Roberson il 24 Feb 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]}

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by