MATLAB Answers

getting rid of empty cells in a cell array

214 views (last 30 days)
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!

  0 Comments

Sign in to comment.

Accepted Answer

Niklas Nylén
Niklas Nylén on 15 Jan 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)

More Answers (1)

Mischa Kim
Mischa Kim on 15 Jan 2014
Edited: Mischa Kim on 15 Jan 2014
Try y=x(~cellfun('isempty',x))

  1 Comment

Sign in to comment.

Sign in to answer this question.