MATLAB: How to get a specific time frame from datetime

datetime

I have data on this time frame:
Time = [11-Sep-2019 08:10:45' 11-Sep-2019 09:10:45' '11-Sep-2019 18:10:47' '11-Sep-2019 20:10:48' '11-Sep-2019 23:10:49'];
I would like to have only from 11-Sep-2019 09:00:00 till 11-Sep-2019 19.00:00
How can I filter this?

Best Answer

Time = datetime({'11-Sep-2019 08:10:45' '11-Sep-2019 09:10:45' '11-Sep-2019 18:10:47' '11-Sep-2019 20:10:48' '11-Sep-2019 23:10:49'});
start_time = datetime('11-Sep-2019 09:00:00');
end_time = datetime('11-Sep-2019 19:00:00');
Time(isbetween(Time, start_time, end_time))
If you are working with timetable objects, see also timerange()
Notice the end time is coded here as 19:00 not as 19.00 as you had posted. If you need 19.00 as input, then you need to decide whether the input will be consistently using the period in that location, or whether either one might occur. If either one might occur then you will want to do string editing to conditionally replace period with colon; if only the period will occur then you could do string editing or you could use an 'InputFormat' option in the datetime() call. Notice that you used the colon in specifying the start hour; that suggests that you want to be able to specify either period or colon in that location.