Interpolate time series data for the same time period on each day

4 views (last 30 days)
Hi,
I'm new to Matlab, so please forgive me if I'm asking an easy question.
I have created a timetable TT1 for minutely interpolation,
TT1 = timetable(DateTime,Close);
for example:
'02-Jan-1990 09:31:00' 353.40
'02-Jan-1990 09:32:00' 353.25
'02-Jan-1990 10:33:00' 353.02
'02-Jan-1990 12:34:00' 352.97
'02-Jan-1990 15:59:00' 352.81
Then I used retime to interpolate data for the first period
DateTime1=[datetime('02-Jan-1990 09:31:00'):minutes(1):datetime('02-Jan-1990 15:59:00')];
TT2 = retime(TT1,DateTime1,'linear');
However, I got more than 6000 trading days to do this 'minutely' interpolation.
The results should be a timetable with a column 'Close' for 6000 days (which is not linear) from 09:31 to 15:59
Is there any better ways than repeatedly typing in the same codes with 6000 different Dates?
Thanks for your help!
  2 Comments
jonas
jonas on 13 Aug 2018
One way would be to interpolate over the entire data set and then remove certain values based on time of day. You would have to temporarily store a timetable 4x the final size though.
dpb
dpb on 13 Aug 2018
Are there any base data before interpolation outside the start/stop times?
I've not tried before with timetable but I'm thinking could use grouping variables on the day as one method.
Alternatively, I think one could use the above idea and simply build the times for datetime programmatically for all days found in the table.
The former would be a little more automatic I think...let me go piddle for a few minutes and see.

Sign in to comment.

Accepted Answer

jonas
jonas on 13 Aug 2018
Edited: jonas on 14 Aug 2018
Here's one way to do it.
%%Original timetable, 5 minute intervals
t=[datetime('1990-1-1 00:00'):minutes(5):datetime('1990-1-8 00:00')]';
t(day(t)==3)=[];
t(day(t)==6)=[];
TT=timetable(t,[1:length(t)]');
%%List of unique days
uDays=unique(datetime(year(t),month(t),day(t)));
%%New time-series 1 min interval over 2 days
t_new=[datetime('1990-1-1 00:00'):minutes(1):datetime('1990-1-8 00:00')]';
Days=datetime(year(t_new),month(t_new),day(t_new));
%%Remove irrelevant times
t_new(~ismember(Days,uDays))=[];
t_new(hour(t_new)<9)=[];
t_new(hour(t_new)>16)=[];
t_new(hour(t_new)==9 & minute(t_new)<30)=[];
%%Interp
retime(TT,t_new,'linear')
I think this should give you what you want. Only problem is you need to store a datetime-array with 8 million rows temporarily (~ 60 MB)
  9 Comments
Peiying LI
Peiying LI on 14 Aug 2018
Jonas, I tried the code and it works fine! except that it still includes times after 16:00 to 17:00.
I changed 16 to 15 like follows:
t_new(hour(t_new)>15)=[];
so it works perfect on 6000 days.
Thank you so much and I would accept your answer!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!