Error when using interpolation method
7 views (last 30 days)
Show older comments
Dears,
I have a dataset, includes variables serial, year, month, day, second, and price. I want to calculate the 5 minute frequency, using last tick interpolation. When I reaech to the last step, the serial numbers are changed. The codes here are provided by Star Strider.
T1 = readtable('Data.txt');
% (1) is the serial, (2) year, (3) month, (4) days, (5) time in seconds, and (6) price
T1.Properties.VariableNames = {'serial','year','month','day','seconds','price'}
% T1(end-4:end,:)
DT = datetime(T1{:,[2 3 4]}) + seconds(T1{:,5});
T2 = table(DT,T1{:,1},T1{:,6}, 'VariableNames',{'time','serial','price'})
TT2 = table2timetable(T2);
TT2 = retime(TT2,'regular','linear','Timestep',minutes(5))
A simple and results that I got is attached. Could you guide me to get the code accurately.
6 Comments
Star Strider
on 28 Oct 2023
What calculation is necessary to produce the result you want?
I suspect that whatever calculation is is being appliied to the price is being applied to the serial numbers as well, and that is the reason they are changing. The serial numbers probably only need to be interpolated to match the five-minute intervals, and not have any calculations applied to them.
Accepted Answer
Star Strider
on 28 Oct 2023
Edited: Star Strider
on 28 Oct 2023
The serial numbers change position because timetable arrays require that the time vector be the first column, regardless of how the original table was constructed.
The serial numbers themselves do not change. (If they were included in the computations, one approach would simply be to remove them, calculate the timetable, and then re-insert them afterwards, interpolated separately to create an appropriate lrow size. However here they are simply interpolated, since that is all this code does.)
Example —
T1 = readtable('Data.txt');
% (1) is the serial, (2) year, (3) month, (4) days, (5) time in seconds, and (6) price
T1.Properties.VariableNames = {'serial','year','month','day','seconds','price'}
% T1(end-4:end,:)
DT = datetime(T1{:,[2 3 4]}) + seconds(T1{:,5});
% T2 = table(DT,T1{:,1},T1{:,6}, 'VariableNames',{'time','serial','price'})
T2 = table(T1{:,1},DT,T1{:,6}, 'VariableNames',{'serial','time','price'})
T2(end-4:end,:)
TT2 = table2timetable(T2)
TT2 = retime(TT2,'regular','linear','Timestep',minutes(5))
TT2(end-4:end,:)
T3 = readtable('Result.txt') % Second File
T3(end-4:end,:)
Time = datetime(T3.Var1, 'InputFormat','''''dd-MMM-yyyy') + timeofday(datetime(T3.Var2,'InputFormat','HH:mm:ss'''''));
T3 = removevars(T3,[1 2]);
T3 = addvars(T3, Time,'Before','Var3')
T3(end-4:end,:)
The second file (‘Result.txt’) appears to be entirely different form the first. The variables are not defined, so I have no idea what they are.
.
6 Comments
Star Strider
on 29 Oct 2023
Thank you!
The last tick would be:
Last = T2(end,:)
or:
Last = TT2(end,:)
depending on what you want.
The same approach works for any of the other table or timetable arrays.
More Answers (0)
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!