time string into seconds

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

Risposte (3)

Star Strider
Star Strider il 21 Feb 2019

0 voti

See if the seconds (link) function will do what you want. Your times need to be a duration (link) array.

1 Commento

Star Strider
Star Strider il 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.

Accedi per commentare.

George1990
George1990 il 21 Feb 2019

0 voti

Schermata 2019-02-21 alle 15.23.08.png

5 Commenti

Walter Roberson
Walter Roberson il 21 Feb 2019
Please show us data1.Time(1073:1074)
George1990
George1990 il 21 Feb 2019
basically at that time the day changes, so I should create a command that tells me when the day change add 1 to the column. I don't know how though
Walter Roberson
Walter Roberson il 21 Feb 2019
Ah, it is not exactly that the day changes, but rather than the time of day of the new entry becomes before the time of day of the first entry.
Working with datetime / duration is easier to get right.
George1990
George1990 il 21 Feb 2019
what do you mean specifically?
Walter Roberson
Walter Roberson il 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.

Accedi per commentare.

Peter Perkins
Peter Perkins il 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

Categorie

Tag

Richiesto:

il 21 Feb 2019

Risposto:

il 14 Mar 2019

Community Treasure Hunt

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

Start Hunting!

Translated by