Adding rows when missing seconds in a time serie
显示 更早的评论
I have a data time serie that looks like this:
2015.03.05 00:00:00 2.3
2015.03.05 00:00:01 2.34
2015.03.05 00:00:02 2.41
2015.03.05 00:00:03 2.53
2015.03.05 00:00:04 2.43
2015.03.05 00:00:05 2.44
2015.03.05 00:00:06 2.36
2015.03.05 00:00:07 2.41
2015.03.05 00:00:12 2.43
2015.03.05 00:00:15 2.41
The time serie is sampled for every second but sometimes there is a jump, for example between 00:00:07 and 00:00:12. Where there is a jump in time I would like to add a row with the missing second and with the value from the second before, for example adding the row 2015.03.05 00:00:08 2.41. Does anyone know how to do this?
回答(2 个)
Assuming, thet input is a cell array (it takes some time to edit the input, such that it is usable for testing. This time is wasted, if the assumption obout the input format is wrong):
[EDITED, Input format adjusted according to comment, 08.02.2017 14:12 UTC]
Date = {'2015.03.05 00:00:00', ...
'2015.03.05 00:00:01', ...
'2015.03.05 00:00:02', ...
'2015.03.05 00:00:03', ...
'2015.03.05 00:00:04', ...
'2015.03.05 00:00:05', ...
'2015.03.05 00:00:06', ...
'2015.03.05 00:00:07', ...
'2015.03.05 00:00:12', ...
'2015.03.05 00:00:15'};
Value = {2.3; 2.34; 2.41; 2.53; 2.43; 2.44; 2.36; 2.41; 2.43; 2.41};
Time = datenum(Date);
Sec = round((Time - Time(1))* 86400) + 1;
Index = zeros(1, Sec(end));
Index(Sec) = 1;
Index = cumsum(Index);
FullTime = cellstr(datestr(Time(1) + (0:Sec(end)-1) / 86400));
FullValue = Value(Index);
Peter Perkins
2017-2-8
0 个投票
If you have R2016b, timetables and the retime method, called with 'secondly' and 'FillWithMissing', does exactly this.
Even prior to R2016b, I think you'd be better off using datetimes and their intersect method. Create a datetime vector that has all the times, and a numeric vector the same size, filled with NaNs. Call intersect to find the locations in the "complete" vector of the elements of the "partial" vector, and assign the "partial" data values into those locations of the full data vector.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!