How to get a specific time frame from datetime?

2 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2019-12-18
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 个评论
Kofial
Kofial 2019-12-18
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 2019-12-18
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);

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by