getting rid of empty cells in a cell array

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 日

2 投票

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 日

3 投票

Try y=x(~cellfun('isempty',x))

4 件のコメント

Sebastiano delre
Sebastiano delre 2014 年 1 月 15 日
thanks, this helps me as well...
Kanthaswamy Ganapathy
Kanthaswamy Ganapathy 2021 年 5 月 4 日
Thank you . How do I extend this to work for a n-d cell array
Yiqian Qian
Yiqian Qian 2021 年 5 月 27 日
I have the same question above, how to apply this to a specific row or colums.
Nisha Bharti
Nisha Bharti 2021 年 10 月 10 日
Yes, same query

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by