Time in hr min sec to seconds

6 visualizaciones (últimos 30 días)
Lenny
Lenny el 9 de Sept. de 2019
Editada: Lenny el 10 de Sept. de 2019
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

Respuestas (4)

Stephen23
Stephen23 el 9 de Sept. de 2019
Editada: Stephen23 el 9 de Sept. de 2019
>> str = '152605';
>> vec = sscanf(str,'%2d');
>> out = [60*60,60,1]*vec
out = 55565
  3 comentarios
Stephen23
Stephen23 el 9 de Sept. de 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
Bruno Luong el 9 de Sept. de 2019
No preference really, your method is clear and readable.
I just want to point out time counting is nothing but base-60 numbers.

Iniciar sesión para comentar.


Bruno Luong
Bruno Luong el 9 de Sept. de 2019
round((datenum('152605','HHMMSS')-datenum('000000','HHMMSS'))*86400)

Bruno Luong
Bruno Luong el 9 de Sept. de 2019
polyval(str2num(reshape('152605',2,3)'),60)

Bruno Luong
Bruno Luong el 9 de Sept. de 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)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by