MATLAB Answers

finding multiple datetime cells from a table

2 views (last 30 days)
Is there a way that I can find a week of data from a table by inputing only a starting date (2020/04/06) and finding the corresponding next 6 days
The table looks something like this:
126540 2020/04/06 00:08 0 5.7
126541 2020/04/06 00:23 0 6.9
126542 2020/04/06 00:38 0 8.2
126543 2020/04/06 00:53 0 8.9
126544 2020/04/06 01:19 1 11.0
126551 2020/04/06 02:23 0 10.6
126740 2020/04/07 05:16 0 10.9
126741 2020/04/07 05:39 1 10.9
126743 2020/04/07 05:31 0 11.2
126963 2020/04/08 10:24 0 14.6
126964 2020/04/08 10:39 0 14.4
etc.
And my code:
T = readtable('testdata.txt');
k = find(ismember(T.Var2,'2020/04/06')); %find the date
k2 = find(ismember(T.Var2,'2020/04/07'));
k3 = find(ismember(T.Var2,'2020/04/08'));
k4 = find(ismember(T.Var2,'2020/04/09'));
k5 = find(ismember(T.Var2,'2020/04/10'));
k6 = find(ismember(T.Var2,'2020/04/11'));
k7 = find(ismember(T.Var2,'2020/04/12'));
I need to extract all the rows within that week by just inputing the starting day of that week.
would be much appreciated with any help.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Aug 2021
Convert to timetable and use a timerange() https://www.mathworks.com/help/matlab/ref/timerange.html
The end date would be the start date + days(6)

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!