Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Matlab function to convert elapsed seconds to HH:MM:SS format?

Tags:

matlab

I would like to convert an elapsed number of seconds into HH:MM:SS format. Is there a built-in function for this, or do I have to write my own?

like image 995
robguinness Avatar asked Aug 31 '12 07:08

robguinness


People also ask

How do you convert seconds to HH MM SS?

Find the number of whole hours by dividing the number of seconds by 3,600. The number to the left of the decimal point is the number of whole hours. The number to the right of the decimal point is the number of partial hours. Then, convert the remaining partial hours to minutes by multiplying the partial hours by 60.

How do I convert time to seconds in Matlab?

S = seconds( X ) returns an array of seconds equivalent to the values in X . If X is a numeric array, then S is a duration array in units of seconds. If X is a duration array, then S is a double array with each element equal to the number of seconds in the corresponding element of X .


3 Answers

datestr is probably the function you are looking for. Express your time interval as a decimal fraction of a day, for example:

>> datestr(0.25, 'HH:MM:SS.FFF')

ans =

06:00:00.000    

That is, one quarter of a day is 6 hours. If you want to transform intervals longer than a day this way you'll have to adjust the second argument, which formats the function's output, for example:

>> datestr(2.256789741, 'DD:HH:MM:SS.FFF')

ans =

02:06:09:46.634

The first argument to datestr could also be either a date vector or a date string rather than a date serial number. This should get you started, if you have problems ask another question or edit this one.

--
To convert a time in seconds using datestr, divide the value by 24*60*60.

Sample:

t1 = toc;
timeString = datestr(t1/(24*60*60), 'DD:HH:MM:SS.FFF');
like image 158
High Performance Mark Avatar answered Oct 29 '22 03:10

High Performance Mark


I don't know a built-in function. However, there is a SEC2HMS on Matlab's File Exchange. Basically, it boils down to something like

function [hours, mins, secs] = sec2hms(t)
    hours = floor(t / 3600);
    t = t - hours * 3600;
    mins = floor(t / 60);
    secs = t - mins * 60;
end

If you also want to have it formatted, use a printf:

function hms = sec2hms(t)
    hours = floor(t / 3600);
    t = t - hours * 3600;
    mins = floor(t / 60);
    secs = t - mins * 60;
    hms = sprintf('%02d:%02d:%05.2f\n', hours, mins, secs);
end

sec2hms(69.9904)
ans =
    00:01:09.99
like image 34
Mehrwolf Avatar answered Oct 29 '22 04:10

Mehrwolf


If you want to get the hours, minutes and seconds as doubles consider the following line of code:

seconds = 5000;
hms = fix(mod(seconds, [0, 3600, 60]) ./ [3600, 60, 1])

hms =

 1    23    20

This line of code is more than 100 times faster than using the built-in datestr funciton.

nIterations = 10000;

tic
for i = 1:nIterations
    hms = fix(mod(seconds, [0, 3600, 60])./[3600, 60, 1]);
end
sprintf('%f ms\r', toc / nIterations * 1000)

gives 0.001934 ms.

tic
for i = 1:nIterations
    datestr(seconds/24/3600, 'HH:MM:SS');
end
sprintf('%f ms\r', toc / nIterations * 1000)

gives 0.209402 ms.

like image 45
Andi Avatar answered Oct 29 '22 03:10

Andi