Calculate difference between two times

조회 수: 21(최근 30일)
Debbie_R
Debbie_R 2012년 6월 21일
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
Debbie_R
Debbie_R 2012년 6월 21일
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

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

채택된 답변

Star Strider
Star Strider 2012년 6월 21일
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
Debbie_R
Debbie_R 2012년 6월 25일
Thank you Star Strider - that worked a treat!
Many thanks,
Debbie

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

추가 답변(3개)

Thomas
Thomas 2012년 6월 21일
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
Nathaniel
Nathaniel 2012년 6월 22일
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', ',');

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


Richard
Richard 2012년 6월 21일
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
Debbie_R
Debbie_R 2012년 6월 21일
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

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


Kevin Holst
Kevin Holst 2012년 6월 21일
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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by