MATLAB Answers

creating date for data sorting in time series data

2 views (last 30 days)
Hi everyone,
I have a time series of sea level with unevenly time interval as you can see in the following:
10-03-2014 17:05 -26.86
10-03-2014 17:07 -26.85
10-03-2014 17:09 -26.85
10-03-2014 17:11 -26.86
10-03-2014 17:13 -26.85
10-03-2014 17:15 -26.87
10-03-2014 17:17 -26.86
10-03-2014 17:19 -26.86
10-03-2014 17:21 -26.86
10-03-2014 17:23 -26.86
10-03-2014 17:25 -26.86
10-03-2014 17:27 -26.85
10-03-2014 17:29 -26.86
10-03-2014 17:31 -26.84
10-03-2014 17:33 -26.85
10-03-2014 17:35 -26.86
10-03-2014 17:41 -26.86
10-03-2014 17:45 -26.87
10-03-2014 17:47 -26.88
10-03-2014 17:49 -26.87
10-03-2014 17:55 -26.87
10-03-2014 17:57 -26.84
10-03-2014 17:59 -26.86
10-03-2014 18:01 -26.83
10-03-2014 18:03 -26.83
10-03-2014 18:05 -26.83
10-03-2014 18:07 -26.84
10-03-2014 18:11 -26.84
10-03-2014 18:13 -26.86
now I want to have data in every 10 minutes. I think that it's good that I generate the data every 1 minutes and after that I just keep the data every 10 minutes. so I want to generate the data every one minutes and if we do not have data in that time the code write nan instead.
Kindly someone helps how to do this. Many many thanks in advance.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Nov 2017
If you have R2016b or later, it might be easiest to create a timetable() and use retime()

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 5 Nov 2017
Presuming that the time information is stored in a cell array of character vectors named "times", then
times = {
'10-03-2014 17:05'
'10-03-2014 17:07'
'10-03-2014 17:09'
'10-03-2014 17:11'
'10-03-2014 17:13'
'10-03-2014 17:15'
'10-03-2014 17:17'
'10-03-2014 17:19'
'10-03-2014 17:21'
'10-03-2014 17:23'
'10-03-2014 17:25'
'10-03-2014 17:27'
'10-03-2014 17:29'
'10-03-2014 17:31'
'10-03-2014 17:33'
'10-03-2014 17:35'
'10-03-2014 17:41'
'10-03-2014 17:45'
'10-03-2014 17:47'
'10-03-2014 17:49'
'10-03-2014 17:55'
'10-03-2014 17:57'
'10-03-2014 17:59'
'10-03-2014 18:01'
'10-03-2014 18:03'
'10-03-2014 18:05'
'10-03-2014 18:07'
'10-03-2014 18:11'
'10-03-2014 18:13'};
sea_levels = [
-26.86
-26.85
-26.85
-26.86
-26.85
-26.87
-26.86
-26.86
-26.86
-26.86
-26.86
-26.85
-26.86
-26.84
-26.85
-26.86
-26.86
-26.87
-26.88
-26.87
-26.87
-26.84
-26.86
-26.83
-26.83
-26.83
-26.84
-26.84
-26.86];
dn = datenum(times);
min_time = min(dn);
min_time_dv = datevec(min_time);
min_time_dv(5) = floor(min_time_dv(5) / 10) * 10; %move to beginning of 10 minute slot
first_slot_dn = datenum(min_time_dv);
max_time = max(dn);
max_time_dv = datevec(max_time);
max_time_dv(5) = floor(max_time_dv(5) / 10) * 10 + 10; %move to end of 10 minute slot
last_slot_dn = datenum(max_time_dv);
ten_mins_as_days = 1 / (24 * 60 / 10);
slot_dns = first_slot_dn : ten_mins_as_days : last_slot_dn;
slot_ds = datestr(slot_dns);
[~, slot_idx] = histc(dn, slot_dns);
mean_sea_levels = accumarray(slot_idx, sea_levels(:), [length(slot_dns)-1,1], @mean, nan);
output = [cellstr(slot_ds(1:end-1,:)), num2cell(mean_sea_levels)];
Mina Masoud
Mina Masoud on 6 Nov 2017
Thank you so much. I really appreciate that.
Walter Roberson
Walter Roberson on 6 Nov 2017
Note: due to round-off error in constructing the slot boundaries, slot_dns, , it is possible that times very close to a ten-minute boundary might get put into the wrong side of the boundary. If that was important, it could be compensated for.

Sign in to comment.

More Answers (0)

Sign in to answer this question.