How do i remove empty cells in a cell array?

Hello,
I have a cell array, see figure below, I want to remove the cell with [] (they are empty). I know this will cause to have different length but that is okay for what i want to do. I have tried different ways but it either removes the entire of the 2nd row or it removes the entire of column 6 to 10. I want to keep all the data in the exact location and just remove the cells with [].
Does anyone know how to only remove column 6,7,8,9 and 10 in only row 2?
Thank you.

6 Comments

" Does anyone know how to only remove column 6,7,8,9 and 10 in only row 2?"
No one knows this, because it is not possible: arrays cannot have holes / gaps / jagged edges.
An two dimensional cell array, like yours, has to have an entry for each of its m by n elements. As in your case some of these elements can be assigned empty matrices. If you really need to keep different length collections of 47 x 15 matrices, you will need to find a different way to store them.
Please provide some further context regarding your actual application and how you will be using the data, for ideas about how to handle your situation.
okay so my main goal is to try and create another cell array with only the 1st columns and 5th columns from the each cell. i tried to do a loop but since row 2 does not have any value after column 5 it breaks.
osc_a_data is the name of the cell array from the picture i have provided. 6x10 cell.
The issue i am having is that the Nsheet is always 10 due to [] but i need it to stop at 5 for row 2 onwards
for i=1:Nfile
Nsheet=length(osc_a_data(i,:));
for j=1:Nsheet
xy{i,j}=osc_a_data{i,j}(:,1);
% end
end
i hope this make sense.
Thank you for helping.
"..my main goal is to try and create another cell array with only the 1st columns and 5th columns from the each cell. i tried to do a loop but since row 2 does not have any value after column 5 it breaks"
You need to decide: what should go into the "another cell array" if the source does not have five columns?
We cannot decide this for you.
..."create another cell array with only the 1st columns and 5th columns from the each cell."
What does that mean, precisely? It isn't consistent with the attempted code even without the complication introduced by using the dangerous length function (have 5 more rows of data in osc_a_data and it will return the number of rows, not columns, be precise in what dimension it is you want).
The precise description above would be simply
xy=osc_a_data(:,[1,5]);
the first and fifth columns of the original.
The ambiguity arises from do you mean the two cell array columns or the content of the cell?
I don't think i explained it well enough, sorry. I just wanted to extract all the data from the 1st and 5th column from the inside of each cell of osc_a_data. So i meant the content of the cell.
I did figured out a way to do so below.
[nrows,ncols]=cellfun(@size,osc_a_data)
for i=1:Nfile
Nsheet=nnz(nrows(i,:))
for j=1:Nsheet
xy{i,j}(:,1)=osc_a_data{i,j}(:,1);
xy{i,j}(:,2)=osc_a_data{i,j}(:,5);
end
end

Sign in to comment.

 Accepted Answer

Your description is not clear to me. See if this is what you are intending.
% Get size of input data
[rows, columns] = size(osc_a_data)
% Create output cell array.
xy = cell(rows, columns)
abort = false;
% Scan every cell taking the 1st and 5th column from each cell
% and putting those into a new cell array called xy.
for row = 1 : rows
for col = 1 : columns
% First see if we encountered an empty cell.
% Quit if we did.
if isempty(osc_a_data{row, col})
% "The issue i am having is that the Nsheet is always 10 due to []
% but i need it to stop at 5 for row 2 onwards"
abort = true;
break;
end
% Extract the 47x15 double matrix from this cell.
cellContents = osc_a_data{row, col};
% "create another cell array with only the 1st columns and 5th columns from the each cell"
newMatrix = cellContents(:, [1, 5]);
% Put the new matrix into new xy cell array.
xy{row, col} = newMatrix;
end
if abort
% Crop new xy cell array so that it does not include any rows
% with or below the first row where we found an empty cell.
xy = xy(1:row-1, :);
break;
end
end

1 Comment

Sorry about the description, still trying to learn the correct terms.
Yes, this is what i wanted but i did remove the following lines of the code to get what i want.
if abort
% Crop new xy cell array so that it does not include any rows
% with or below the first row where we found an empty cell.
xy = xy(1:row-1, :);
break;
end
Thank you.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Asked:

S C
on 3 Aug 2023

Edited:

dpb
on 3 Aug 2023

Community Treasure Hunt

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

Start Hunting!