Deleting data within row of cells

1 view (last 30 days)
Marissa Gabriel
Marissa Gabriel on 19 Oct 2021
Commented: Marissa Gabriel on 20 Oct 2021
(Semi advanced beginner here)
I have a list of cells (1x18) that have subsequent tables. Each table has a varied number of rows, but they all have their first bunch of rows are "0" (like 80% of the data so I want to delete to be able to work with the nonzeroed values). Once the values change to nonzero, they never return to zero.
I'm trying to extract the values to delete all the zeros and I've tried different means and not having success, splitting subarrays, etc. Any ideas for how to continue? I want to be able to work with a set of columns for nonzero values to do some analysis.

Answers (1)

Image Analyst
Image Analyst on 19 Oct 2021
Did you try a for loop? Here is untested code (because you forgot to attach your data):
for k = 1 : numel(C)
% Extract the table.
thisCellContents = C{k};
% Find the first non-zero item in the first column of the table.
firstNonZeroRow = find(thisCellContents{:, 1} ~= 0, 1, 'first')
if ~isempty(firstNonZeroRow)
% Take rows from that point on.
thisCellContents = thisCellContents((firstNonZeroRow + 1):end, :);
% Stick updated table back into cell.
C{k} = thisCellContents;
If you still need more help, attach your cell array in a .mat file with the paperclip icon.
save('answers.mat', 'C');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!