MATLAB: How to extract rows by date

extract data with same variables

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,:)
...

Best Answer

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 !