Matlab 去除空元胞数组内容

Matlab Remove empty cell array content

我有 5*14 矩阵 (m*n)

M={'a','b',.....' ',' ' ;
   'aa','bb',...' ',' ' ;
    ... }

删除空单元格后删除空单元格的最佳选项是 (5*n):

M={'a','b','c';
   'aa','bb' ;
   'e';
   'aa','xx';
   ...}

当我这样做时:

emptyCells = cellfun('isempty', M);
cols = size(M,2);
M(emptyCells) = [];
M = reshape(M, [], cols);

我收到错误:Error using reshape Product of known dimensions, 14, not divisible into total number of elements, 52.

这是创建嵌套单元格的解决方案:

M={'a','b','c';'d','e','';'','','';'','f',''};
%Convert cell to nested cell
M=mat2cell(M,ones(size(M,1),1));
%now apply your code to every subcell
for ix=1:numel(M)
    emptyCells = cellfun('isempty', M{ix});
    M{ix}(emptyCells)=[];
end