MATLAB: Elapsed Time for specific month

time

i have dataset as follow 1976/1/30 20:45:48.81 1976/2/2 3:58:35.29 1976/2/3 10:3:31.11 1976/2/3 13:14:40.79 1976/2/6 16:24:49.9 1976/2/16 10:9:0 1976/3/16 10:9:36.8 1976/3/23 3:34:1.2 1976/3/26 5:12:39.4 1976/4/26 11:19:0 1976/4/26 11:19:26.77 1976/5/26 21:50:20 1976/5/2 5:1:14.5 1976/5/3 18:25:0 1976/6/3 18:25:17.25 1976/7/12 23:28:0 1976/7/12 23:28:20.7 1976/7/14 4:10:20 1976/7/19 14:14:8.12 1976/7/20 18:53:1.2 1976/7/22 20:52:0 1976/7/22 20:52:51.92 1976/7/23 15:58:18.92 1976/7/24 17:58:0 1976/8/24 17:58:39.35 1976/8/25 6:33:31.49 i want to group the data monthly.after that i want to find elapsed time of specific month, first time and last time… the data is from 1976 to 2013…

Best Answer

Better in the future to attach a properly-formatted file.
Here's a solution, using a table, datetimes, and splitapply. The result is a duration in units of days:hours: minutes:seconds.
>> dstr = { ...
'1976/1/30 20:45:48.81'
'1976/2/2 3:58:35.29'
'1976/2/3 10:3:31.11'
'1976/2/3 13:14:40.79'
'1976/2/6 16:24:49.9'
'1976/2/16 10:9:0.0'
'1976/3/16 10:9:36.8'
'1976/3/23 3:34:1.2'
'1976/3/26 5:12:39.4'
'1976/4/26 11:19:0.0'
'1976/4/26 11:19:26.77'
'1976/5/26 21:50:20.0'
'1976/5/2 5:1:14.5'
'1976/5/3 18:25:0.0'
'1976/6/3 18:25:17.25'
'1976/7/12 23:28:0.0'
'1976/7/12 23:28:20.7'
'1976/7/14 4:10:20.0'
'1976/7/19 14:14:8.12'
'1976/7/20 18:53:1.2'
'1976/7/22 20:52:0.0'
'1976/7/22 20:52:51.92'
'1976/7/23 15:58:18.92'
'1976/7/24 17:58:0.0'
'1976/8/24 17:58:39.35'
'1976/8/25 6:33:31.49'};
>> timestamp = datetime(dstr,'InputFormat','uuuu/M/d H:m:s.SS');
>> timestamp.Format = 'uuuu/MM/dd HH:mm:ss.SS';
>> t = table;
>> [g,t.Year,t.Month] = findgroups(timestamp.Year,timestamp.Month);
>> t.Min = splitapply(@min,timestamp,g);
>> t.Max = splitapply(@max,timestamp,g);
>> t.Elapsed = t.Max - t.Min;
>> t.Elapsed.Format = 'dd:hh:mm:ss'
Year Month Min Max Elapsed
____ _____ ______________________ ______________________ ____________
1976 1 1976/01/30 20:45:48.81 1976/01/30 20:45:48.81 00:00:00
1976 2 1976/02/02 03:58:35.29 1976/02/16 10:09:00.00 14:06:10:24
1976 3 1976/03/16 10:09:36.80 1976/03/26 05:12:39.40 09:19:03:02
1976 4 1976/04/26 11:19:00.00 1976/04/26 11:19:26.77 00:00:26
1976 5 1976/05/02 05:01:14.50 1976/05/26 21:50:20.00 24:16:49:05
1976 6 1976/06/03 18:25:17.25 1976/06/03 18:25:17.25 00:00:00
1976 7 1976/07/12 23:28:00.00 1976/07/24 17:58:00.00 11:18:30:00
1976 8 1976/08/24 17:58:39.35 1976/08/25 06:33:31.49 12:34:52
Had to add ".0" to the strings that had no fractional seconds part.