2
votes

I have a cellarray with two columns and several rows. When I use

data = data(~cellfun('isempty', data)) 

It removes the empty cells and creates a cell array with both the previous two rows now in one row. I want them to still be in two rows. How could I do this?

To be clearer:

data{i, 1} = subdata_1
data{i, 2} = subdata_2 

where subdata_1 and 2 are further cell arrays. So the cell array data will contain two columns and several rows, where each cell is another cell array. Some rows will be empty [] and when I remove these empty cells, data will no longer contain two columns and several rows, but only 1 column. How can I keep the N x 2 structure of the data cell array?

1
Your problem is not entirely clear. Please be a bit more specific and try to post an example. - UJIN

1 Answers

2
votes

When you remove only one column from a row, the result is going to be a vector rather than a 2 x N array because MATLAB is unable to determine the dimensionality.

a = [1, 2, 3; 4 5 6].';
size(a)
%   3   2

a = a(a ~= 4);
size(a)
%   5   1

What you'll want to do instead is to remove entire rows. Now to determine which rows depends upon what behavior you expect.

So if we setup some example data:

data = {1, 2, []; [], 3, []}.';
%   [1]    []
%   [2]   [3]
%    []    []

If you want to remove any row that has an empty cell

result = data(~any(cellfun('isempty', data), 2), :);
%   [2]   [3]

If you want to remove rows that have all empty cells

result = data(~all(cellfun('isempty', data), 2), :);
%   [1]    []
%   [2]   [3]