MATLAB: How to convert hours, minutes, seconds to seconds

time

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?

Best Answer

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