フィルターのクリア

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 Center および File ExchangeDates and Time についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by