How can I extract a specific time for a "datetime" table?
11 views (last 30 days)
Show older comments
Dear coders,
I have a simple question but I am confused how to code it. I have this datetime table (attached with the question: RESULT.mat) and I want to keep only those rows that contains time between 2:00-3:00 am and 12:00-13:00 pm. As shown in this picture -
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1296525/image.png)
The final datetime table should contain only these rows with the desired HourSeries. Any feedback from you will be highly appreciated! <3
0 Comments
Accepted Answer
Kevin Holly
on 15 Feb 2023
Edited: Kevin Holly
on 15 Feb 2023
load('RESULT.mat')
result
result.HourSeries = datetime(result.HourSeries,"Format","HH:mm")
index = hour(result.HourSeries)==12|hour(result.HourSeries)==2;
result(index,:)
More Answers (2)
Sulaymon Eshkabilov
on 15 Feb 2023
Here is one partial answer that finds the data pointsat specific times, i.e. 02:00, 03:00, 12:00, 13:00 and overlooks other time points such as s12:08, for example:
R =load('RESULT.mat').result;
IDX1 = find(ismember(R.HourSeries, '02:00'));
IDX2 = find(ismember(R.HourSeries, '03:00'));
IDX3 = find(ismember(R.HourSeries, '12:00'));
IDX4 = find(ismember(R.HourSeries, '13:00'));
Rs = R([IDX1,IDX2, IDX3, IDX4, IDX4],:); % Selected
whos R Rs
dpb
on 15 Feb 2023
result.HourSeries=duration(result.HourSeries,'InputFormat','hh:mm');
ix=isbetween(result.HourSeries,hours(2),hours(4))|isbetween(result.HourSeries,hours(12),hours(13));
result=result(ix,:);
Keeps only the selected entries; you lose the rest and would have to reload the .mat file if need any other data. Doing the conversion and reSAVEing first would probably be the smart thing; then save that step going forward.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!