Filter löschen
Filter löschen

converting hhmmss to seconds

11 Ansichten (letzte 30 Tage)
Daniel
Daniel am 18 Okt. 2015
Bearbeitet: Stephen23 am 18 Okt. 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 Kommentar
Daniel
Daniel am 18 Okt. 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;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 18 Okt. 2015
Bearbeitet: Stephen23 am 18 Okt. 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

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by