converting hhmmss to seconds

11 次查看(过去 30 天)
I have a file with the time in the following format hhmmss.ss. So for example, 175012.76 is 17 hours, 50 minutes and 12.76 seconds. I'm reading the file in using dlmread. How do I convert that to seconds? Thanks
  1 个评论
Daniel
Daniel 2015-10-18
Maybe not the most elegant, but I finally did this:
time_wind = dlmread([path,file]); %read in 25 Hz wind file
time_all = num2str(time_wind(:,1)*100);
time_sec = str2num(time_all(:,5:8))/100;
time_min = str2num(time_all(:,3:4));
time_hr = str2num(time_all(:,1:2));
time_sfm = 3600*time_hr + 60*time_min + time_sec;
time_wind(:,1)=time_sfm;

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-10-18
编辑:Stephen23 2015-10-18
Although you do not give any useful information at all in your question about the file format or how many of these numbers there are and how they might be arranged, here are two possible solutions for you to try out.
Method One: from decimal value
Simply convert it into hours, minutes and seconds, and then sum them:
>> val = 175012.76;
>> hrs = floor(val/10000)
hrs = 17
>> mns = floor(rem(val,10000)/100)
mns = 50
>> scs = rem(val,100)
scs = 12.7600000000093
>> 60*60*hrs+60*mns+scs
ans = 64212.76
Method Two: read the file as characters
It might be easier to read the file using textscan, which would allow you to import the values as strings, which can then be parsed directly into hours, minutes and seconds. This is much faster and more efficient than separate calls to str2num, cellfun or the like:
>> str = '175012.76'
str = 175012.76
>> vec = sscanf(str,'%2d%2d%f')
vec =
17
50
12.76
>> sum([60*60;60;1].*vec)
ans = 64212.76

更多回答(0 个)

类别

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