How to extract rows by date?
12 views (last 30 days)
Show older comments
say I have a table A
18-Dec-2015 42.3000 29.9000 7.3000
18-Dec-2015 37.3000 30.1000 13.4000
18-Dec-2015 39.1000 30.0300 6.5000
18-Dec-2015 42.3000 29.9000 7.3000
19-Dec-2015 37.3000 30.1000 13.4000
19-Dec-2015 39.1000 30.0300 6.5000
20-Dec-2015 42.3000 29.9000 7.3000
20-Dec-2015 39.1000 30.0300 6.5000
20-Dec-2015 37.3000 30.1000 13.4000
I have almost 3000 days to do. How could I use a loop to extract rows by time and do some calculation separately? Thanks in advance!!! I tried ismember and the following one but neither works.
for i = (:,1)
if A(i,1)=A(i+1,1)
C=A(i,:)
...
2 Comments
Jan
on 12 May 2018
It would be useful to post some code, which creates the example data or to attach the variable as MAT file. This would clarify, which format the "table" has exactly.
What is the purpose of "for i = (:,1)"? "Neither works" is less useful than posting the error message.
Accepted Answer
Akira Agata
on 12 May 2018
It's not clear what you want to do, but, as Star-san mentioned, retime function would be your help. Let me start assuming your table A has 4 columns and the 1st column is stored as datetime, like:
>> A
A =
9×4 table
Time Var1 Var2 Var3
__________ ____ _____ ____
2015/12/18 42.3 29.9 7.3
2015/12/18 37.3 30.1 13.4
2015/12/18 39.1 30.03 6.5
2015/12/18 42.3 29.9 7.3
2015/12/19 37.3 30.1 13.4
2015/12/19 39.1 30.03 6.5
2015/12/20 42.3 29.9 7.3
2015/12/20 39.1 30.03 6.5
2015/12/20 37.3 30.1 13.4
To use retime function, first you should convert your table into "timetable" by using table2timetable function.
>> A = table2timetable(A);
>> A
A =
9×3 timetable
Time Var1 Var2 Var3
__________ ____ _____ ____
2015/12/18 42.3 29.9 7.3
2015/12/18 37.3 30.1 13.4
2015/12/18 39.1 30.03 6.5
2015/12/18 42.3 29.9 7.3
2015/12/19 37.3 30.1 13.4
2015/12/19 39.1 30.03 6.5
2015/12/20 42.3 29.9 7.3
2015/12/20 39.1 30.03 6.5
2015/12/20 37.3 30.1 13.4
Now you can use retime function to do some calculation separately for each day. For example, you can calculate daily average for each variable (Var1~Var3) in 1 line, like:
>> retime(A,'daily','mean')
ans =
3×3 timetable
Time Var1 Var2 Var3
__________ ______ ______ ______
2015/12/18 40.25 29.983 8.625
2015/12/19 38.2 30.065 9.95
2015/12/20 39.567 30.01 9.0667
I hope my post somehow helps you !
4 Comments
Peter Perkins
on 14 May 2018
Iris, you really want to turn Date into a datetime, not text, and turn cp_flag into categorical, not text. Your life will be much easier.
More Answers (1)
See Also
Categories
Find more on Timetables 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!