How do I detect empty cells in a cell array?

心不动则不痛 提交于 2019-11-28 18:36:55

Use CELLFUN

%# find empty cells
emptyCells = cellfun(@isempty,a);
%# remove empty cells
a(emptyCells) = [];

Note: a(i)==[] won't work. If you want to know whether the the i-th cell is empty, you have to use curly brackets to access the content of the cell. Also, ==[] evaluates to empty, instead of true/false, so you should use the command isempty instead. In short: a(i)==[] should be rewritten as isempty(a{i}).

Mahdi Karami

All above mentioned answers are incorrect, because in my case when i used them, they removed empty cells and then all elements of my cell array situated in a row manner instead of preserving their actual shape. In fact after using this kind of approach your cell array elements tend to be a row cell vector.

I have found this approach which works correctly in my case.

source : https://groups.google.com/forum/#!topic/comp.softsys.matlab/p3NX0fI6u90

approach:

myCellARRAY(all(cellfun(@isempty,myCellARRAY),2), : ) = [];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!