Filling in missing data with previous data to perform calculations

2 views (last 30 days)
Emma Atkinson on 28 Sep 2021
Commented: Emma Atkinson on 30 Sep 2021
Hi. I have a set of Sp02 data from a 3-day time period that I am using to calculate hypoxic debt. The data is stored in a single array and I am dividing up the array into specific time chunks of different lengths and performing calculations on each chunk of data. In certain sections of each chunk, there are larger sections of erroneous values that I want to fill in with previous non-erroneous values so I can have calculations over the entire duration of the data. I want to replace these sections of bad data with the last previous value before the erroneous data starts, which will change depending on where the section of bad data is located in the array. If there are multiple sections of erroneous values spread out throughout the array of data, how can I extract the last value before the bad data starts and replace the bad data with that single value? **The bad data are values that are equal to 500 or go below 60.
Example: The bad section of data runs from 1 to 500. I have sections of bad data from 10 to 30, 150 to 175, 220 to 240 and 380 to 410. I want to take the values from cell 9, 149, 219, and 379 and put those values in for the bad section i.e. the value in cell 9 would fill in 10 to 30, the value in cell 149 would fill in 150 to 175, and so on.... How would I go about this?
Thank you!

Scott MacKenzie on 28 Sep 2021
Since you define bad or erroneous data as values equal to 500 or values less than 60, how about this. Replace the erroneous values with nan and then use the fillmissing function to copy the preceding value into the erroneous positions.
If the data are in a vector d, then
d(d==500) = nan;
d(d<60) = nan;
dnew = fillmissing(d, 'previous');
Emma Atkinson on 30 Sep 2021
This worked perfectly. Thank you!