How can I calculate the delta time for each line?
Show older comments
15:27:02.563904
15:27:02.815706
15:27:03.067059
I have this kind of over a 1000 data transfer line time hh:mm:ss:ms
How can I calculate the delta time for each line???
2 Comments
LuKr
on 1 Aug 2018
If you got your time stamps stored in cell array A you can transform it to seconds like this:
[~, ~, ~, H, MN, S] = datevec(A);
timeInSeconds = 3600.*H+60.*M+S;
The delta time is
deltaTime = diff(timeInSeconds);
Samueal, has your question been answered? If not, provide feedback instead of starting a new question.
Answers (3)
It is important to use valid matlab syntax when describing your data so it's not ambiguous what exactly you have (and also it's important to format your post properly, I've done that for you this time). It's unclear if your data comes as a 2D char array, a cell array of char vectors, a string array, a column vector of datenum, an array of date vectors, a datetime array, or something else I haven't thought about.
If your data is not a datetime array, then convert it to that. It is then trivial to get the difference between each date with
tdiff = diff(yourdatetimearray)
which will return a duration array. If you want the duration in seconds, then:
seconds(tdiff)
Walter Roberson
on 1 Aug 2018
TS = {'15:27:02.563904'
'15:27:02.815706'
'15:27:03.067059'};
deltaT = diff(duration(TS, 'Format', 'hh:mm:ss.SSSSSS'))
seconds(deltaT)
deltaT will be the time differences in duration format, and seconds() of that will turn those into pure numeric seconds
2 Comments
samueal mekonnen
on 1 Aug 2018
Adam Danz
on 1 Aug 2018
There's no need for a loop in any of these solutions. For all 3 solutions proposed here (1 in the comments section to your question) all you need to do is plug in your vector of date/time values into the code.
Peter Perkins
on 3 Aug 2018
Beginning in R2018a, you can convert directly to durations:
>> duration(["15:27:02.563904" "15:27:02.815706" "15:27:03.067059" "15:27:03.318437" "15:27:03.569846"],'Format','hh:mm:ss.SSSSSS')
ans =
1×5 duration array
15:27:02.563903 15:27:02.815706 15:27:03.067059 15:27:03.318436 15:27:03.569846
Then follow Walter's and Guillaume's advice to use diff.
2 Comments
Walter Roberson
on 3 Aug 2018
I thought I was converting directly to durations in my answer?
Peter Perkins
on 6 Aug 2018
whoops, you were. my bad.
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!