Calculate difference between two times

5 次查看(过去 30 天)
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 个评论
Thomas
Thomas 2012-6-21
where are you getting numbers like 183946 from the format mentioned above (20080102 095648)?
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.

更多回答(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 个评论
Debbie_R
Debbie_R 2012-6-21
Hi Thomas,
Thank you very much for this solution.
I am currently using the function importdata to read in the data from the csv file. The file has 3 header lines which have different delimiters to the data part, so I found importdata the easiest way to handle the import and separate out the header lines. This function saves the data in the format MyData.data and when I save Time as, e.g. Time = MyData.Data(:,2), it is formatted as a Double.
Do you know if it would be possible to convert my existing format into a string after the data has been imported and then follow the concatenation method you describe?
Thanks again,
Debbie
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

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by