Is there a way to linearly interpolate dates and times?

24 次查看(过去 30 天)
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
Kye Taylor 2012-7-3
编辑:Kye Taylor 2012-7-3
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 个评论
Kye Taylor
Kye Taylor 2012-7-5
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.

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by