How do i remove empty cells in a cell array?
Show older comments
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
Stephen23
on 3 Aug 2023
" 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.
Jon
on 3 Aug 2023
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.
S C
on 3 Aug 2023
Stephen23
on 3 Aug 2023
"..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?
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!