how to find time difference between 2 times , in milliseconds.

조회 수: 29 (최근 30일)
rnd rnd
rnd rnd 2018년 10월 23일
댓글: Peter Perkins 2018년 10월 31일
So I have a cell with a column with data like this :
.....
'18:42:45.813'
'18:42:46.028'
'18:42:46.139'
'18:42:46.359'
'18:42:46.463'
'18:43:06.558'
'18:43:06.779'
....
I want to find out the time differences lets say between 2 rows,
I tried it like this: etime(datevec(datenum(c{1,2}{1,1})), datevec(datenum(c{1,2}{2,1})))
it worked great when I did not have milliseconds, my data was like this:
.....
'18:42:25'
'18:42:36'
'18:42:46'
....
but if I add data with milliseconds it gives me a wrong answer.
I want the result in milliseconds (also in seconds if possible).

채택된 답변

jonas
jonas 2018년 10월 23일
편집: jonas 2018년 10월 23일
Just work with duration format.
t={'18:42:45.813'
'18:42:46.028'
'18:42:46.139'
'18:42:46.359'
'18:42:46.463'
'18:43:06.558'
'18:43:06.779'};
td = duration(t,'inputformat','hh:mm:ss.SSS');
out = milliseconds(diff(td))
ans =
215
111
220
104
20095
221
The only problem is if your time is displaying time of day, in which case you need to use datetime instead, or else you will get a large spike in your time difference at midnight.
  댓글 수: 10
rnd rnd
rnd rnd 2018년 10월 24일
Nvm , I just use milliseconds (td(5) - td(1)) :) thank you so much , you helped me out a lot.
Peter Perkins
Peter Perkins 2018년 10월 31일
In earlier versions, you can convert from text to duration using datevec, or using datetime+timeofday, but perhaps even easier, you can use text2duration on the FEX.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 10월 23일
https://in.mathworks.com/help/matlab/ref/duration.milliseconds.html#buovb4_-2_1 convert the time array to milliseconds as suggested in the link and then do the calculations

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by