How can I calculate the delta time for each line?

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

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.

Sign in to comment.

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)
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

Is there anyway you can represent the time duration with loop.. since the data line is over a 1000.
Thank you for you help!
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.

Sign in to comment.

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

I thought I was converting directly to durations in my answer?
whoops, you were. my bad.

Sign in to comment.

Categories

Tags

Asked:

on 1 Aug 2018

Commented:

on 6 Aug 2018

Community Treasure Hunt

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

Start Hunting!