필터 지우기
필터 지우기

converting hhmmss to seconds

조회 수: 11 (최근 30일)
Daniel
Daniel 2015년 10월 18일
편집: Stephen23 2015년 10월 18일
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