Calculate difference between two times

35 views (last 30 days)
Hello,
I would be very grateful if someone could assist me with the following problem.
I have a data file with one column that supplies dates in the form yyyymmdd and time in another column in the form hhmmss. Please see example below:
20080102 095648
When I read the data into Matlab it is saved in the format Double (i.e. not as a string), e.g.
Time(1:10)
ans =
183946
183950
183954
183958
184002
I would like to calculate the time difference between consecutive rows in seconds and then save the results in Double format in an array. I need to account for the fact that the time can go past midnight. However, I am having great difficulty doing doing any date/time calculations because of the format (i.e. not in standard dd/mm/yyyy hh:mm:ss format). It would be great if I could perform the operation using indices, i.e. not in a for loop where I have to subtract each row in sequence.
Thanks in advance for any suggestions.
/Debbie
  2 Comments
Debbie_R
Debbie_R on 21 Jun 2012
Hi Thomas,
Sorry, I should have been clearer in my question about the data format. The time and date are actually stored in different columns, so the 20080102 095648 shown is just a copy and paste of two adjacent columns from the csv data file. So I read time and date in separately and they are stored in separate arrays in Matlab in Double format (not a string). The Time(1:10) example above is from much further down the data file than the first example (6pm rather than 9am). I was just trying to demonstrate how the data is formatted once read into Matlab - sorry for the confusion.
Many thanks,
Debbie

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 21 Jun 2012
I suggest:
for k1 = 1:size(Date,1)
DateStr(k1,:) = sprintf('%8d', Date(k1));
TimeStr(k1,:) = sprintf('%06d', Time(k1));
FullDateVct(k1,:) = datevec([DateStr(k1,:) TimeStr(k1,:)], 'yyyymmddHHMMSS');
FullDateNum(k1) = datenum(FullDateVct(k1,:));
end
This is kludgy but when I played around with it, it worked. If all goes well, you only need to do it once. The matrix brackets I used in the 'datevec' call automatically concatanate the strings.
I added FullDateVct to provide readable dates as a check to be sure everything works as you want it to. It's probably best to store the dates and times as a single 'datenum' column and convert them later as necessary.
Then use ‘etime’ to find the difference between two date vectors.
  1 Comment
Debbie_R
Debbie_R on 25 Jun 2012
Thank you Star Strider - that worked a treat!
Many thanks,
Debbie

Sign in to comment.

More Answers (3)

Thomas
Thomas on 21 Jun 2012
You can convert the date string into a date number and get the difference between them
EG.
a =['20080102 095648';'20080102 105748']; % two dates apart 1hr 1min
d=datenum(a,'yyyymmdd HHMMSS'); % convert to number
difference=d(2)-d(1); % difference between the two
datestr(difference,'HH:MM:SS') % difference in hr:min:sec
  4 Comments
Nathaniel
Nathaniel on 22 Jun 2012
The first thing you should do is open Matlab and type:
doc textscan
Then you can use Thomas Anthony's solution above and change:
data=textscan(fid, '%s %s');
to
data=textscan(fid, '%s %s', 'HeaderLines', 3, 'Delimiter', ',');

Sign in to comment.


Richard
Richard on 21 Jun 2012
Its hard to suggest what you can do from your example. I would suggest converting any dates into julian dates when working in matlab and then convert them into a suitable format after. Try using datenum to convert to julian dates:
From your example: when you import the data what appears in your workspace?
Do you have one variable with 20080102 095648 as one entry of a variable or are these divided into two i.e.
time = [20080102, 095648]
If you would provide an example of your data format I would be able to provide more help. I cant see how you get from 20080102 095648 to 183946.
  1 Comment
Debbie_R
Debbie_R on 21 Jun 2012
Hi Lestyn,
Thanks for the advice.
The time and date data are provided in a csv file as separate variables. When I import Time I get (for the first 10 rows):
Time(1:10)
ans =
183946
183950
183954
183958
184002
I get exactly the same format for Date, but, e.g. 20081231.
As mentioned in an answer to Thomas above, I selected two different times in my example above (one from 9am and one from 6pm - sorry I should have used more consistent examples).
Many thanks,
Debbie

Sign in to comment.


Kevin Holst
Kevin Holst on 21 Jun 2012
since you're getting this from a data file I'd suggest something like this:
fid = fopen('data.txt');
data = textscan(fid,'%s %s');
for i = 1:length(data{1})
dates(i) = datenum([data{1}{i} 'T' data{2}{i}],'yyyymmddTHHMMSS');
end
difference = diff(dates);
fclose(fid);
vals = str2num(datestr(difference,'SS')); % this assumes that the times will be less than 1 minute apart

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by