Clear Filters
Clear Filters

How to get a specific time frame from datetime?

3 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 18 Dec 2019
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.
  2 Comments
Kofial
Kofial on 18 Dec 2019
Thank you! It works :).
Was a speed error. I wanted 19:00 as input.
Another short question.
To each time it corresponds a concentration value.
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'];
Concentration = [1.4 0.1 2.4 0.1 0.4];
How can I filter that I get the concentration values for the selected timeframe as well ( 11-Sep-2019 09:00:00 till 11-Sep-2019 19:00:00
Walter Roberson
Walter Roberson on 18 Dec 2019
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'});
Concentration = [1.4 0.1 2.4 0.1 0.4];
start_time = datetime('11-Sep-2019 09:00:00');
end_time = datetime('11-Sep-2019 19:00:00');
mask = isbetween(Time, start_time, end_time);
selected_Time = Time(mask);
selected_Concentration = Concentration(mask);

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!