Effacer les filtres
Effacer les filtres

find elapse time with date rollover

5 vues (au cours des 30 derniers jours)
tybo1mos
tybo1mos le 22 Juil 2021
Commenté : Chunru le 27 Juil 2021
I have a array with a bunch of times formatted as HH:MM:SS as follows:
11:58:00
11:59:00
00:00:02
00:02:04
I'm trying to find the elapse time in seconds from begining time. The time data I have starts a new day (11pm to 12am). All my attemps to use etime or subtract datenum result in returning a negative time when trying to subtract 00:00:02 from 11:58:00. I havn't figured out how to make matlabe know the 00:00:02 is actually a new date, since there is no actual date defined in my data just time.
I would like to get the following result, in seconds from start:
0
60
62
182

Réponses (2)

Peter Perkins
Peter Perkins le 27 Juil 2021
Get away from using datenum. Can't say this strongly enough.
>> tstr=["11:58:00"; "11:59:00"; "00:00:02"; "00:02:04"];
>> tod = duration(tstr)
tod =
4×1 duration array
11:58:00
11:59:00
00:00:02
00:02:04
>> newDay = [false;diff(tod)<0]
newDay =
4×1 logical array
0
0
1
0
Don't know if you klnow what day the time start on. If you cross a Daylight Saving Time shift, it will matter. Use datetime, not datenum.
>> day0 = datetime(2021,7,26);
>> t = day0 + caldays(cumsum(newDay)) + tod
t =
4×1 datetime array
26-Jul-2021 11:58:00
26-Jul-2021 11:59:00
27-Jul-2021 00:00:02
27-Jul-2021 00:02:04
>> elapsed = seconds(t - t(1))
elapsed =
0
60
43322
43444
My answer is different than yours. Guess what? Your time stamps have no way of indicating am or pm. That would seem to be a problem. If you can, use 24-hour time stamps. If not, then how do you know if it's 11 am or pm? about the only thing you can do is assume you will have timestamps in both the first and second half of every day, and my newDay variable will indicate both noon and midnight crossings.
  1 commentaire
Chunru
Chunru le 27 Juil 2021
It seems that here is no foolproof answer to the question where date time info is ambiguous. Just make the best guess.

Connectez-vous pour commenter.


Chunru
Chunru le 22 Juil 2021
tstr=[
"11:58:00"
"11:59:00"
"00:00:02"
"00:02:04"];
t = datenum(tstr, 'HH:MM:SS')
t = 4×1
1.0e+05 * 7.3816 7.3816 7.3816 7.3816
d = diff(t)*24*3600;
% check for negative difference
d(d<0) = d(d<0)+12*3600;
d = [0; d]
d = 4×1
0 60.0000 62.0000 122.0000

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by