Is there a way to linearly interpolate dates and times?

26 views (last 30 days)
I am trying to interpolate discharge data using gage information. The time that corresponds to the discharge data is listed in Excel as:
1/10/08 15:33 (for example)
In MATLAB I've used the datestr command on the data and the dates are now listed as:
11-Jan-0108 15:33:42 (for example)
The data is generally given in 10 minute increments, so my question is as follows: is there a way to linearly interpolate the discharge data so it returns data for every 2 minutes, for example? I've tried using interp1 but I'm not sure exactly how to input the date so it reads properly.

Accepted Answer

Kye Taylor
Kye Taylor on 3 Jul 2012
Edited: Kye Taylor on 3 Jul 2012
If you convert the dates to serial date numbers as the cyclist suggests, then you might be able to benefit from this example...
% Create a dataset that resembles yours
dates = today:10:today+100; % dates as serial date numbers
data = sin(linspace(0,2*pi,length(dates))); % create some fake data
disp('The dates include')
for i = 1:length(dates)
disp(datestr(dates(i)))
end
finerDates = dates(1):2:dates(end); % get a finer grid
% map x-data to 0,1
scaledDates = (dates - min(dates))/(max(dates)-min(dates));
scaledFinerDates = (finerDates - min(finerDates))/(max(finerDates)-min(finerDates));
% get interpolated data -- spline is an option, could be 'linear'
interpolatedData = interp1(scaledDates, data, scaledFinerDates,'spline');
% visualize it
figure,hold on
plot(dates,data,'ro')
plot(finerDates, interpolatedData,'b.-')
legend('original data','interpolated data')
datetick('x');
  3 Comments
Kye Taylor
Kye Taylor on 5 Jul 2012
The serial date numbers are on the order of 10^5. The interpolant that I use is comprised of polynomials (like p(x) = a*x + b*x^2 + c*x^3 -- a cubic spline). One needs to scale the data so that you don't end up squaring or cubing 10^5. as this will result in too wide a range of values to get a meaningful interpolant. If you use 'linear' as the option to interp1, the scaling should make no difference, but if you want a more sophisticated interpolant, you should scale.
Nahid Atashi
Nahid Atashi on 31 Oct 2018
thank you so much Kye. your code was great for me too

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 3 Jul 2012
One method would be to use the datenum() command to convert the dates to pure numbers, which can be consumed by interp1.

Community Treasure Hunt

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

Start Hunting!