time string into seconds

64 次查看(过去 30 天)
George1990
George1990 2019-2-21
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

回答(3 个)

Star Strider
Star Strider 2019-2-21
See if the seconds (link) function will do what you want. Your times need to be a duration (link) array.
  1 个评论
Star Strider
Star Strider 2019-2-21
@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.

请先登录,再进行评论。


George1990
George1990 2019-2-21
Schermata 2019-02-21 alle 15.23.08.png
  5 个评论
George1990
George1990 2019-2-21
what do you mean specifically?
Walter Roberson
Walter Roberson 2019-2-21
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.

请先登录,再进行评论。


Peter Perkins
Peter Perkins 2019-3-14
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by