# Is there a way to linearly interpolate dates and times?

48 views (last 30 days)
Matt on 3 Jul 2012
Commented: Nahid Atashi on 31 Oct 2018
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.

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');
Nahid Atashi on 31 Oct 2018
thank you so much Kye. your code was great for me too

### More Answers (1)

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.
##### 1 CommentShowHide None
Walter Roberson on 3 Jul 2012
"consumed" ?