[~,~,~,hours,minutes,seconds] = datevec('00:26:16:926', 'HH:MM:SS:FFF');
out = 1000*(3600*hours + 60*minutes + seconds);
out =
1576926
在这种格式中, datevec 的输出将是一个6元素向量,分别输出年,月,日,小时,分钟和秒 . 毫秒分辨率将被添加到 datevec 's output, so all you have to do is convert the fourth to sixth elements into milliseconds and add them all up, which is what is done above. If you don'的第六个元素t指定实际日期,它默认为当前年份的1月1日...但我们还没有使用日期...我们只是想要时间!
4 回答
为什么不尝试使用datevec呢?
datevec
旨在接收各种时间和日期字符串,它会解析字符串并为您提供有用的信息 . 没有必要使用regexp
或以任何方式拆分你的字符串 . 这是一个简单的例子:在这种格式中,
datevec
的输出将是一个6元素向量,分别输出年,月,日,小时,分钟和秒 . 毫秒分辨率将被添加到datevec
's output, so all you have to do is convert the fourth to sixth elements into milliseconds and add them all up, which is what is done above. If you don'的第六个元素t指定实际日期,它默认为当前年份的1月1日...但我们还没有使用日期...我们只是想要时间!datevec
的美妙之处在于它可以接受 multiple 字符串,因此您不仅限于单个输入 . 只需将所有字符串放入单个单元格数组中,然后按以下方式使用datevec
:一种解决方案可能是:
结果:
假设您的日期字符串一致地以该格式出现,您可以使用如下这样简单的内容:
输出:
您可以使用
datevec
进行转换和点积来转换带有日期甚至向量的单个字符串