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 Comments

Please post the input data in valid Matlab syntax. It is not clear, if this is a table, a cell array, a text file or a cell string.
It is a csv.file that is imported from an excel-file on a Mac computer.

Sign in to comment.

Answers (2)

Jan
Jan on 8 Feb 2017
Edited: Jan on 8 Feb 2017
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);

2 Comments

This is what I have, only the date and time are in different cells. I cannot seem to get it to work if they are not in the same cell. Do you know how to fix this?
Jan
Jan on 8 Feb 2017
Edited: Jan on 8 Feb 2017
@malinri: Again I've used a minute, to type a guessed format for the input data. Please post the data such that it can be used by copy&paste.
I've modified the code slightly to work with 2 different input cells.

Sign in to comment.

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.

Categories

Asked:

on 8 Feb 2017

Answered:

on 8 Feb 2017

Community Treasure Hunt

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

Start Hunting!