Converting hh:mm:ss into seconds

Hi all,
I want to read the vectors which are hh:mm:ss (14:35:59.812) format and convert it to seconds. How can I do this in matlab?
Best

 採用された回答

Andreas Goser
Andreas Goser 2012 年 1 月 20 日

6 投票

Is this what you are looking for?
t='14:35:59.812';
[Y, M, D, H, MN, S] = datevec(t);
H*3600+MN*60+S

4 件のコメント

tethys
tethys 2012 年 1 月 20 日
編集済み: per isakson 2016 年 12 月 3 日
Thanks for the answer,
My problem was about reading the data as HH:MM:SS.FFF, and my code was:
clear all
clc
[c,pathc]=uigetfile({'*.txt'},'Select the data','Desktop');
file=[pathc c];
A = importdata(file, ' ',5);
dt = datestr(A,'HH:MM:SS,FFF')
and it was reading t='14:35:59.812' as t='14:35:59.000', but now I realized my problem, and solved it.
Thanks again :)
qiang zhang
qiang zhang 2016 年 12 月 3 日
I have the same problem. the datestr does not read miliseconds in this format. could you explain how you solve this?
per isakson
per isakson 2016 年 12 月 3 日
On R2016a I get the expected result
>> dt = datestr( now,'HH:MM:SS,FFF')
dt =
12:58:26,797
Arshey Dhangekar
Arshey Dhangekar 2021 年 9 月 1 日
I want to convert Time column ( HH:MM:SS) to min, there is a direct command time2num but it requires toolbox and it's paid.So without using time2num how can I convert into minutes. I tried with datetime also but I got error.
table=readtable('sample.csv'):
times = datetime(table.Time,'InputFormat','HH:mm:ss:SSS');
times = hour(times).*60 + minute(times) + second(times)./60;

サインインしてコメントする。

その他の回答 (3 件)

per isakson
per isakson 2012 年 1 月 20 日

2 投票

The first and the last cell returns the result I think you are looking for. See help on DATENUM
datenum( '14:35:59.812', 'HH:MM:SS.FFF' ) .* (24*60*60) - ...
datenum( '00:00:00.000', 'HH:MM:SS.FFF' ) .* (24*60*60)
datestr( datenum( '00:00:00.000', 'HH:MM:SS.FFF' ), 'yyyy-mm-dd' )
datenum( '14:35:59.812', 'HH:MM:SS.FFF', 0 ) .* (24*60*60)

1 件のコメント

tethys
tethys 2012 年 1 月 20 日
Thansk for the answer, it is very useful information:)

サインインしてコメントする。

Peter Seibold
Peter Seibold 2021 年 4 月 18 日

1 投票

About 100 times faster is:
t='14:35:59.812';
seconds=sum(sscanf(t,'%f:%f:%f').*[3600;60;1]);
Steven Lord
Steven Lord 2021 年 9 月 1 日

0 投票

t='14:35:59.812';
F = 'hh:mm:ss.SSS';
du = duration(t, 'InputFormat', F, 'Format', F)
du = duration
14:35:59.812
format longg % To make s look nicer
s = seconds(du)
s =
52559.812

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2012 年 1 月 20 日

回答済み:

2021 年 9 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by