Plot time series with irregular intervals and interpolate at regular intervals

조회 수: 4 (최근 30일)
Hello all,
I have a matrix which contains data in this format: [ Date Time(HHMMSS) Price ]
2 70052 40.175
2 70214 40
2 70249 40
2 70347 40
2 70400 40
I want to be able to plot this as a time series as is, and then do linear interpolation for some interval (e.g. 15 mins) to get a reduced matrix. How may I be able to do this? I tried plotting the 3rd column but it becomes equally spaced 1 second intervals.
Thank you,

답변 (1개)

Jonathan Epperl
Jonathan Epperl 2012년 11월 11일
You plot minutes vs. price by running (Assuming your data is in the variable M)
plot(M(:,2),M(:,3));
With regards to the interpolation you need to be a little more specific. You fit the best (in the least-squares sense) straight line to your data by running
r = [M(:,2) ones(size(M,1),1)]\M(:,3)
% Check v your Data
plot(M(:,2),M(:,3),'kx',M(:,2),r(1)*M(:,2)+r(2),'r');
legend('Data', 'Linear Interpolation')
so that r(1) is the slope, r(2) the intercept of the line.
  댓글 수: 9
Louise
Louise 2012년 11월 11일
편집: Louise 2012년 11월 11일
With your code, the x-axis still shows minutes/seconds >60. I managed to convert all the times into the datenum format. I don't know if there is something to be done then.
EDIT: Okay nevermind. I managed to use datetick() to change all the dates in datenum() format, so it displays the time correctly. However, I am not sure if using interp1() on the dates in datenum format will mess things up.
Walter Roberson
Walter Roberson 2012년 11월 11일
The code I gave converts everything to seconds, not to minutes/seconds. You need continuous ranges in order to interpolate. The serial date format such as is returned by datenum is fine for interpolation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by