How to create a sequence of datetime for given set of datetime series (with duplicate dates)?

I have table with a column DATETIME of the format ''dd-MM-yyyy HH:mm:ss'' (shown below) and wish to create a sequence of datetime series from 09:30 to 17:00 for each datetime mentioned in column. I have duplicate dates e.g. two datetime in column are "06-01-2010 13:40:00" and "06-01-2010 15:10:00". And i need the separate sequence for each of them.
Please help me on that and I have matlab R2016a version only.
DATETIME
'05-01-2010 15:05:00'
'06-01-2010 13:40:00'
'06-01-2010 15:10:00'
'07-01-2010 16:10:00'
'07-01-2010 16:40:00'
'08-01-2010 13:30:00'
'08-01-2010 13:40:00'
'08-01-2010 13:50:00'
'11-01-2010 16:10:00'
'11-01-2010 16:40:00'
'12-01-2010 16:15:00'
'12-01-2010 16:40:00'
'12-01-2010 16:55:00'
'13-01-2010 14:10:00'
'13-01-2010 15:20:00'
'13-01-2010 16:25:00'
'14-01-2010 15:50:00'
'14-01-2010 16:20:00'
'14-01-2010 16:30:00'
'14-01-2010 16:40:00'

2 Comments

Since you are working with timestamped data timetable would be a better datatype over table. You could convert your table into a timetable first using table2timetable function. After that you can easily achieve this using something like retime
Thanks for suggestions but since I mentioned that I am using R2016a which does not have timetable function. Please provide an alternative solution.

Sign in to comment.

Answers (1)

You can use the following code to find the row index of the required time.
dstr =[...
'05-01-2010 15:05:00'
'06-01-2010 13:40:00'
'06-01-2010 15:10:00'
'07-01-2010 16:10:00'
'07-01-2010 16:40:00'
'08-01-2010 13:30:00'
'08-01-2010 13:40:00'
'08-01-2010 13:50:00'
'11-01-2010 16:10:00'
'11-01-2010 16:40:00'
'12-01-2010 16:15:00'
'12-01-2010 16:40:00'
'12-01-2010 16:55:00'
'13-01-2010 14:10:00'
'13-01-2010 15:20:00'
'13-01-2010 16:25:00'
'14-01-2010 15:50:00'
'14-01-2010 16:20:00'
'14-01-2010 16:30:00'
'14-01-2010 16:40:00'];
dn = datetime(dstr, 'InputFormat', 'dd-MM-yyyy HH:mm:SS');
[hh, mm, ss] = hms(dn);
idx = datetime(2010, 1, 1, hh, mm, ss) >= datetime(2010, 1, 1, 09, 30, 0) ...
& datetime(2010, 1, 1, hh, mm, ss) <= datetime(2010, 1, 1, 17, 00, 0);
dstr(idx, :)
If your data matrix is arranged in a similar way with dstr. Then you can extract your data:
x = data(idx, :); % assume that you have many columns of data.

4 Comments

Thanks for trying but I am not getting the desired result so from above DATETIME column.
And what if I have DATETIME as colume of a table ?
I am not getting the desired result so from above DATETIME column i need all timestamp from 09:30 to17:00 for each DATETIME like
This is output I need - (just showing for first three DATETIME rows)
'05-01-2010 09:30:00'
'05-01-2010 09:31:00'
'05-01-2010 09:32:00'
.
.
.
'05-01-2010 17:00:00'
'06-01-2010 09:30:00'
'06-01-2010 09:31:00'
'06-01-2010 09:32:00'
'06-01-2010 09:33:00'
.
.
.
'06-01-2010 17:00:00'
'06-01-2010 09:30:00'
'06-01-2010 09:31:00'
'06-01-2010 09:32:00'
'06-01-2010 09:33:00'
.
.
.
'06-01-2010 17:00:00'
.
.
Please provide any help on this with R2016a.
Can you provide your data as a table in that case?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2016a

Asked:

NS
on 30 May 2021

Commented:

on 1 Jun 2021

Community Treasure Hunt

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

Start Hunting!