getting rid of empty cells in a cell array

29 次查看(过去 30 天)
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!

采纳的回答

Niklas Nylén
Niklas Nylén 2014-1-15
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)

更多回答(1 个)

Mischa Kim
Mischa Kim 2014-1-15
编辑:Mischa Kim 2014-1-15
Try y=x(~cellfun('isempty',x))
  4 个评论
Yiqian Qian
Yiqian Qian 2021-5-27
I have the same question above, how to apply this to a specific row or colums.

请先登录,再进行评论。

类别

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