Calculate difference between two times
显示 更早的评论
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
采纳的回答
更多回答(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 个评论
Debbie_R
2012-6-21
Thomas
2012-6-21
import the two columns fromt he csv files
Eg> dateTime.csv contains
20080102 095648
20080102 105748
20080103 095648
20080103 105748
Then use the following the Matlab script to import and find difference
fid=fopen('dateTime.csv');
data = textscan(fid,'%s %s');
dtTime=strcat(data{:,1}, data{:,2});
d=datenum(dtTime,'yyyymmddHHMMSS');
difference=diff(d);
datestr(difference,'HH:MM:SS')
Debbie_R
2012-6-21
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
0 个投票
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
类别
在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!