Plot time series with irregular intervals and interpolate at regular intervals

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 日
Hello, I've tried simply plotting the 2nd column against the 3rd column but the x axis includes the points when the minutes of the time (HHMMSS) is greater than 60 minutes, eg. 76100 (7:61:00) or 87100 (8:71:00) and I don't know how to get rid of it.
Jonathan Epperl
Jonathan Epperl 2012 年 11 月 11 日
Could you elaborate on that? The middle column is supposed to be HHMMSS, but there are entries where the 2nd to 3rd digits are >59? How could that happen? Do you want to simply discard those points, or do you want to convert those so they make sense?
Matt Fig
Matt Fig 2012 年 11 月 11 日
Jonathan, read the time from the last digit. The leading 0 is absent....
70052 = 07:00:52
Jonathan Epperl
Jonathan Epperl 2012 年 11 月 11 日
Matt, like I was saying, 2nd to 3rd digits are >59? Still, how is that possible if those are actually hours, minutes and seconds without the separators?
Matt Fig
Matt Fig 2012 年 11 月 11 日
Oh I missed the first comment. I thought you were seeing that in the OP.
Louise
Louise 2012 年 11 月 11 日
So this is what my plot looks like:
I do not have any points where the minutes or seconds are > 60, but the graph includes those times in the axis and I'm not sure how I can get rid of it.
seconds = fix(M(:,2)/10000) * 60 * 60 + fix(mod(M(:,2),10000) / 100) * 60 + mod(M(:,2),100);
plot(seconds, M(:,3))
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Dates and Time についてさらに検索

タグ

質問済み:

2012 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by