Time in hr min sec to seconds
2 views (last 30 days)
Show older comments
I have a struct where the time field is written in hr min sec format with no dividers. For example, 152605 is 15:26 and 5 seconds. I’m trying to convert it to overall seconds but when I use datevec it gives me weird values, where the first row is equal to [417 10 25 0 0 0]. Is there a specific function for this? Can I pull out the number of hrs and mins to convert to seconds and add to the number of seconds in the time column?
I need to be able to do this by referencing the position in the struct, as there are 171 time stamps (I cannot input the time stamp manually). Desired outputs would be: different fields added to the struct that represent hours, mins, sec; one field added to the struct that represents the number of seconds equal to the time in hours, mins, seconds
The field in the struct that contains this data is data.time
Answers (4)
Stephen23
on 9 Sep 2019
Edited: Stephen23
on 9 Sep 2019
>> str = '152605';
>> vec = sscanf(str,'%2d');
>> out = [60*60,60,1]*vec
out = 55565
3 Comments
Stephen23
on 9 Sep 2019
@Bruno Luong: I guess I am a bit old-fashoined, but I do like how this code uses only very basic and efficient operations.
Bruno Luong
on 9 Sep 2019
No preference really, your method is clear and readable.
I just want to point out time counting is nothing but base-60 numbers.
Bruno Luong
on 9 Sep 2019
round((datenum('152605','HHMMSS')-datenum('000000','HHMMSS'))*86400)
0 Comments
Bruno Luong
on 9 Sep 2019
Purely arithmetics
x=str2double('152605');
h=floor(x/1e4);
y=mod(x,1e4);
m=floor(y/1e2);
s=mod(y,1e2);
nsec=polyval([h,m,s],60)
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!