How to remove rows of zero's occurring prior to rows of desired data?

1 view (last 30 days)
I have a 2432132x14 timetable, and the first 390000 rows and last 109000 are filled with zeros. I would like to make a new timetable that removes the 390000 rows and 109000 rows and was planning on approaching this by writing a script that simply removes all rows of data full of zero’s. However, there are some rows of 0’s that occur within the data that I want to keep in the new timetable. How can I define in the code that I only want to remove rows of 0’s before and after the desired data appears? For context, this is biological signal data (EEG and others) collected during surgery. The long period of inactivity at the beginning is because the devices have not been turned on yet, and any periods of inactivity that occur after turning on the devices must be kept for analysis. Currently, I have manually defined the data to be included in the populated table, but would like to develop a more generalizable approach that accounts for variations in file sizes and timings when different case data is loaded.

Answers (1)

Adam Danz
Adam Danz on 30 Jan 2023
Edited: Adam Danz on 30 Jan 2023
This demo makes the following assumptions
  1. The first row of non-zeros is the first row of the data you'd like to isolate.
  2. The last row of non-zeros is the last row of data you'd like to isolate.
TT = array2timetable([zeros(4,5);rand(3,5);zeros(2,5);rand(3,5);zeros(6,5)], ...
'RowTimes',datetime(2000,1,1)+days(0:17))
TT = 18×5 timetable
Time Var1 Var2 Var3 Var4 Var5 ___________ _______ ________ ________ _______ ________ 01-Jan-2000 0 0 0 0 0 02-Jan-2000 0 0 0 0 0 03-Jan-2000 0 0 0 0 0 04-Jan-2000 0 0 0 0 0 05-Jan-2000 0.49298 0.54929 0.27728 0.26681 0.058687 06-Jan-2000 0.96314 0.062153 0.76806 0.67639 0.054733 07-Jan-2000 0.26611 0.68003 0.013848 0.4605 0.16643 08-Jan-2000 0 0 0 0 0 09-Jan-2000 0 0 0 0 0 10-Jan-2000 0.93874 0.3222 0.36095 0.73968 0.43329 11-Jan-2000 0.24747 0.20651 0.37453 0.56352 0.37971 12-Jan-2000 0.70144 0.98603 0.62913 0.31974 0.15416 13-Jan-2000 0 0 0 0 0 14-Jan-2000 0 0 0 0 0 15-Jan-2000 0 0 0 0 0 16-Jan-2000 0 0 0 0 0
isAllZeros = all(TT{:,:}==0,2)
isAllZeros = 18×1 logical array
1 1 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
% find first row that is not all 0s
a = find(~isAllZeros,1,'first')
a = 5
% find last row that is not all 0s
b = find(~isAllZeros,1,'last')
b = 12
% Remove select rows of zeros
TT([1:a,b+1:end],:) = []
TT = 7×5 timetable
Time Var1 Var2 Var3 Var4 Var5 ___________ _______ ________ ________ _______ ________ 06-Jan-2000 0.96314 0.062153 0.76806 0.67639 0.054733 07-Jan-2000 0.26611 0.68003 0.013848 0.4605 0.16643 08-Jan-2000 0 0 0 0 0 09-Jan-2000 0 0 0 0 0 10-Jan-2000 0.93874 0.3222 0.36095 0.73968 0.43329 11-Jan-2000 0.24747 0.20651 0.37453 0.56352 0.37971 12-Jan-2000 0.70144 0.98603 0.62913 0.31974 0.15416

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!