How can I find the time difference between each consecutive rows?

I have a column in which date and time is mentioned, I want to find the time difference between every consective rows . Also how can I find the time difference in seconds , minutes, hours , days , weeks , months and years.
TimeStamp ; TimeDiff;
2014-09-02 07:48:09.567; NULL;
2014-09-02 07:51:02.810 ; 00:03:00
2014-09-02 08:06:13.387 ; 00:15:00
2014-09-02 08:37:09.647; 00:31:00
2014-09-02 14:32:00.113 ; 05:55:00
2014-09-02 16:16:42.593; 01:44:00

 採用された回答

Stephen23
Stephen23 2018 年 7 月 3 日
編集済み: Stephen23 2018 年 7 月 3 日

0 投票

Convert to datetime. The use caldiff (or diff).

8 件のコメント

Ammy
Ammy 2018 年 7 月 3 日
Would you please elaborate?
Stephen23
Stephen23 2018 年 7 月 3 日
編集済み: Stephen23 2018 年 7 月 3 日
@Noor Bano: Please explain if these dates are in a file, a table, a cell array, or a char array. If they are in a file, write what kind of file, and how you are importing that data?
If those dates are in a file then you can import the dates as datetime: both textscan and readtable support this, if you have a new enough MATLAB version. What version of MATLAB are you using?
Stephen23
Stephen23 2018 年 7 月 3 日
Noor Bano's "Answer" moved here:
I am using 2013 a, and this data is in excel file , after importing the data I have a column vector in matlab , how can I find the time difference between each row and what is the format of date and time for finding the difference.
Stephen23
Stephen23 2018 年 7 月 3 日
"I am using 2013 a"
Aha, so you can't use datetime anyway. Good to know.
"after importing the data I have a column vector in matlab"
I presume that by "column vector" you have a cell array of char vectors.
Ammy
Ammy 2018 年 7 月 3 日
Is there any other way ?
Stephen23
Stephen23 2018 年 7 月 3 日
編集済み: Stephen23 2018 年 7 月 3 日
You could convert to datenum, diff, and then convert to a string representation. Assuming that you have a cell array of char vectors C, something like this might work:
N = datenum(C,'yyyy-mm-dd HH:MM:SS');
D = diff(N);
datestr(D,'HH:MM:SS')
Ammy
Ammy 2018 年 7 月 3 日
Thank you very much. But please If I want the time difference interval in only seconds or minutes then is there any way ?
Stephen23
Stephen23 2018 年 7 月 3 日
編集済み: Stephen23 2018 年 7 月 3 日
@Noor Bano: yes, you can easily calculate this yourself. Serial date numbers (e.g. the output from datenum) are given in days, so...
  • Multiply the days by 24 to get hours (there are 24 hours in 1 day).
  • Multiply again by 60 to get minutes (there are 60 minutes in 1 hour).
  • Multiply again by 60 to get seconds (there are 60 seconds in 1 minute).
For example, to get the differences in minutes:
>> C = {'2014-09-02 07:48:09.567';'2014-09-02 07:51:02.810';'2014-09-02 08:06:13.387';'2014-09-02 08:37:09.647';'2014-09-02 14:32:00.113';'2014-09-02 16:16:42.593'};
>> N = datenum(C,'yyyy-mm-dd HH:MM:SS.FFF');
>> D = diff(N);
>> D*24*60
ans =
2.8874
15.1763
30.9377
354.8411
104.7080

サインインしてコメントする。

その他の回答 (1 件)

M.Devaki Mohanarangam
M.Devaki Mohanarangam 2023 年 9 月 8 日

0 投票

Time = [16/04/2023 14:44:22
16/04/2023 14:44:26
16/04/2023 14:44:31
16/04/2023 14:44:36
16/04/2023 14:44:42
16/04/2023 14:44:46
16/04/2023 14:44:51
16/04/2023 14:44:56
16/04/2023 14:45:02
16/04/2023 14:45:06 ]
how to find time difference in loop condtion? plz help

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2018 年 7 月 3 日

回答済み:

2023 年 9 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by