getting rid of empty cells in a cell array

32 visualizaciones (últimos 30 días)
Sebastiano delre
Sebastiano delre el 15 de En. de 2014
Comentada: Nisha Bharti el 10 de Oct. de 2021
Hi,
can you please help me with this? I have the following cell array
x = {1,[],'ciao',[],[]};
I want to exclude the empty cells, and get another array like this:
y = {1,'ciao'};
Thank you!

Respuesta aceptada

Niklas Nylén
Niklas Nylén el 15 de En. de 2014
First, check which cells that are empty using the function isempty. Since isempty does not accept cell arrays as input, you can use the cellfun function. This will call a function with each element of a cell array:
index = cellfun(@isempty, x) == 0;
y = x(index)

Más respuestas (1)

Mischa Kim
Mischa Kim el 15 de En. de 2014
Editada: Mischa Kim el 15 de En. de 2014
Try y=x(~cellfun('isempty',x))
  4 comentarios
Yiqian Qian
Yiqian Qian el 27 de Mayo de 2021
I have the same question above, how to apply this to a specific row or colums.
Nisha Bharti
Nisha Bharti el 10 de Oct. de 2021
Yes, same query

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by