How to convert hours, minutes, seconds to seconds?

Hi,
How can I return an array with
hh:min:sec
into
seconds?
For example
VarName2(1:5)
ans =
'14:54:25'
'14:54:25'
'14:54:25'
'14:54:26'
'14:54:26'
into a array with seconds?

 채택된 답변

Daniel
Daniel 2014년 10월 16일

2 개 추천

Assuming you mean seconds in the day
% initialize variables
NUM_SECONDS_PER_DAY = 86400.0;
timeStrings = {'14:54:25';'14:54:25';'14:54:25';'14:54:26';'14:54:26'};
% convert times to fractional days using datenum
timeFractionalDays = datenum(timeStrings);
% leave only the part with the most recent day fraction
timeDayFraction = mod(timeFractionalDays,1);
% multiply by number of seconds in a day
timeInSeconds = timeDayFraction .* NUM_SECONDS_PER_DAY
This produces the result
timeInSeconds =
1.0e+04 *
5.3665
5.3665
5.3665
5.3666
5.3666

댓글 수: 8

Lizan
Lizan 2014년 10월 16일
편집: Lizan 2014년 10월 16일
The time string is actually stored in a big vector valled "VameName2". I just gave some numbers for example.
Obtained following error:
Error using datenum (line 179)
DATENUM failed.
Caused by:
Error using datevec (line 104)
The input to DATEVEC was not an array of strings.
When I wrote,
timeStrings = {VarName2};
if VarName2 is a cell array of strings, try substituting VarName2 in for timeStrings like this:
% initialize variables
NUM_SECONDS_PER_DAY = 86400.0;
% convert times to fractional days using datenum
timeFractionalDays = datenum(VarName2);
% leave only the part with the most recent day fraction
timeDayFraction = mod(timeFractionalDays,1);
% multiply by number of seconds in a day
timeInSeconds = timeDayFraction .* NUM_SECONDS_PER_DAY
Lizan
Lizan 2014년 10월 16일
I accepted your answer a bit to early, ..
I plotted the data and I am noticing that the time is starting from 980 min or so.. I expected it to start from zero.
Daniel
Daniel 2014년 10월 16일
Is the first string in VarName2 00:00:00? That is the only time that would return a 0 value from this algorithm, since it is returns seconds in the day. What were you looking to get your answer in terms of?
Lizan
Lizan 2014년 10월 16일
편집: Lizan 2014년 10월 16일
Well, the data is for example in format 14:54:25. I guess I would like to have the time in seconds from the first time entry.
so
'14:54:25' - 0 sec
'14:54:25' - 0 sec
'14:54:25' - 1 sec
'14:54:26' - 2 sec
'14:54:26' - 2 sec
etc
14:54:25 is 53665 seconds
Subtract this number, i.e. 53665 from the calculation of Daniel or my method.
Simply subtract the first value of the answer vector from itself.
% initialize variables
NUM_SECONDS_PER_DAY = 86400.0;
% convert times to fractional days using datenum
timeFractionalDays = datenum(VarName2);
% leave only the part with the most recent day fraction
timeDayFraction = mod(timeFractionalDays,1);
% multiply by number of seconds in a day
timeInSeconds = timeDayFraction .* NUM_SECONDS_PER_DAY;
% find answer starting from first value
timeInSeconds = timeInSeconds - timeInSeconds(1)
Lizan
Lizan 2014년 10월 16일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2023년 8월 21일
편집: Stephen23 2023년 8월 21일
The approach for MATLAB >=R2014b is to use the DURATION class, e.g.:
C = {'14:54:25'; '14:54:25'; '14:54:25'; '14:54:26'; '14:54:26'};
S = seconds(duration(C))
S = 5×1
53665 53665 53665 53666 53666

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

질문:

2014년 10월 16일

편집:

2023년 8월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by