Calculate difference between two times
조회 수: 10 (최근 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
2012년 6월 21일
where are you getting numbers like 183946 from the format mentioned above (20080102 095648)?
채택된 답변
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
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
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
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.
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!