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!