How to fill data gaps in a time series with NaN

5 views (last 30 days)
Dear All,
I am having a time series with some gaps in it and i want to fill the gaps with NaN, how can I do that the.....the interval of my time series is 0.00274
  2 Comments
dpb
dpb on 26 Oct 2014
Are there missing entries or is there some other value present now?
Smith J
Smith J on 27 Oct 2014
I am having the time series....so how to do...I dont need to create any time series

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 26 Oct 2014
I am not certain what you want to do, but if you want to create a continuous time vector and fill the corresponding missing data values with NaN, this works:
Ts = 0.00274; % Sampling Time
t = [0:10 14:20 25:30]*0.00274; % Create Time Series With Gaps
f = rand(size(t)); % Create Matching Data
tn = round(t/Ts); % Create Indices With Gaps
dt = diff([0 tn]); % Create Vector Of Differences
tg = find(dt > 1); % Find Indices Of Gaps
gaps = dt(tg)-1; % Find Lengths Of Gaps
ti = linspace(min(t),max(t),max(tn)); % Create Continuous Time Vector
fi = interp1(t,f,ti); % Create Matching Data Vector
for k1 = 1:length(tg) % Loop Filling Gaps In ‘f’ With NaN
q = [tg(k1):tg(k1)+gaps(k1)-1];
fi(tg(k1):tg(k1)+gaps(k1)-2) = nan(1,gaps(k1)-1);
end
If you also want the time vector to have NaN values (not recommended), add this line after the ‘fi’ assignment in the loop:
ti(tg(k1):tg(k1)+gaps(k1)-1) = nan(1,gaps(k1));
The output of this routine are the time vector ‘ti’ and the data vector ‘fi’.
There are likely different ways to do this. This method seems to do what you want.
  2 Comments
dpb
dpb on 26 Oct 2014
"There are likely different ways to do this"
Aren't there (almost) always? :)
One may be John d' Errico's inpaint_nans File Exchange submittal...
It gets (like all of John's most excellent submissions) rave reviews altho I don't know it'll solve OPs query directly as haven't actually used it meself...
Star Strider
Star Strider on 26 Oct 2014
Nor have I.
I usually just write my own code to solve specific problems, since it’s usually easier than learning someone else’s code and adapting it to my application.

Sign in to comment.

More Answers (0)

Categories

Find more on Genomics and Next Generation Sequencing 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!