To interpolate and replace NaNs in a variable co2_clean
, we can use the built-in function fillmissing
.
Assuming that the co2_clean
variable is a matrix, we can interpolate the NaNs using linear interpolation in columns by calling the function:
main.m47 chars2 lines
This replaces all NaN in columns (i.e., observation times) with linearly interpolated values.
To extrapolate the end values, we can also specify 'EndValues','extrapolate'
in the function call. A complete example follows:
main.m73 chars2 lines
Note that fillmissing
function supports other types of interpolation methods such as spline, pchip, and nearest, which may be better suited for your data depending on the nature of your data.
Additionally, if co2_clean
variable is a table, struct, or a timetable, the fillmissing
function works on the specified variable as well.
Reference:
https://www.mathworks.com/help/matlab/ref/fillmissing.html
gistlibby LogSnag