converting hhmmss to seconds

23 views (last 30 days)
Daniel
Daniel on 18 Oct 2015
Edited: Stephen23 on 18 Oct 2015
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 Comment
Daniel
Daniel on 18 Oct 2015
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;

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 18 Oct 2015
Edited: Stephen23 on 18 Oct 2015
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

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!