time string into seconds

64 views (last 30 days)
George1990
George1990 on 21 Feb 2019
Answered: Peter Perkins on 14 Mar 2019
Hello everyone! I have uploaded a column of time from excel in the format 'HH:MM:SS PM' as a string and I have converted it into Matlab as a datetime with datestr() . I don't know how to transfrom this column of time into seconds, because if I apply the formula : t*24*60*60 -t(1)*24*60*60 , I get the seconds but at a certain row of the vector I start having negative numbers. Could somebody help me? Thank you

Answers (3)

Star Strider
Star Strider on 21 Feb 2019
See if the seconds (link) function will do what you want. Your times need to be a duration (link) array.
  1 Comment
Star Strider
Star Strider on 21 Feb 2019
@George1990 — Did you transform your times into a duration array and then use the seconds function? Times and dates are generally difficult to do yourself. The MATLAB date and time functions make this much easier (although truly comprehensive documentation for the the datetime and related functions is lacking).
Also, see if using the readtable function on your Excel file will make the conversions easier.

Sign in to comment.


George1990
George1990 on 21 Feb 2019
Schermata 2019-02-21 alle 15.23.08.png
  5 Comments
George1990
George1990 on 21 Feb 2019
what do you mean specifically?
Walter Roberson
Walter Roberson on 21 Feb 2019
Suppose the first entry in date1.Time was for Feb 7, 2019, 14:27:32 . Then you would extract the 14:27:32 from that, convert it to seconds, and that would become the time(1) value. Later you would subtract time(1) from that so the first entry of the result would become 0.
Now suppose the second entry was for Feb 7, 2019, 14:27:20 . Then you would extract the 14:27:20 from that, convert it to seconds. You would subtract the seconds converted from 14:27:32 from that, and since 14:27:32 is more seconds into the day, the second entry would come out negative.
Now suppose the third entry was for Feb 8, 2019, 11:18:43. Then you would extract the 11:18:43 from that, convert it to seconds, subtract the seconds appropriate to 14:27:32 from that, getting a negative number. This does correspond to a day change.
Now suppose the fourth entry was for Feb 8, 2019, 15:51:17. Then you would extract the 15:51:17 from that, convert it to seconds, subtract the seconds appropriate to 14:27:32 from that, getting a positive number, but you also had a day change. Therefore you cannot rely upon negative numbers to indicate a day change.
If your entries are strictly increasing and the last one for any day is guaranteed to be after (time of day) the first one for the next day, then there is is a method to make adjustments.
... but it would be easier to use datetime and duration objects to get the calculations right.

Sign in to comment.


Peter Perkins
Peter Perkins on 14 Mar 2019
I think StarStrider and Walter have already answered this, but in case things are not clear. Start from some text timestamps, make a datetime, grab the time portion, and convert to (numeric) seconds.
>> dt = datetime({'04:56:35 pm' '04:59:48 pm'},'InputFormat','hh:mm:ss a')
dt =
1×2 datetime array
14-Mar-2019 16:56:35 14-Mar-2019 16:59:48
>> d = timeofday(dt)
d =
1×2 duration array
16:56:35 16:59:48
>> secondsSinceMidnight = seconds(d)
secondsSinceMidnight =
60995 61188
Or perhaps
>> secondsSinceFirst = seconds(d - d(1))
secondsSinceFirst =
0 193
I would suggest that you may not want to convert to a numeric value. You may just want to change the duration's display format.
>> d.Format = 's'
d =
1×2 duration array
60995 sec 61188 sec

Tags

Community Treasure Hunt

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

Start Hunting!