简体   繁体   English

将Matlab时间戳数据转换为秒(HH:MM:SS:TTT->秒)

[英]Convert Matlab timestamp data to seconds (HH:MM:SS:TTT -> seconds)

My data (cell) in Matlab looks like this: 我在Matlab中的数据(单元格)如下所示:

00.00.00.515
00.00.00.671
00.00.00.828
00.00.00.984
00.00.01.140
etc.

This is the time stamp: HH:MM:SS:TTT, the T stands for thousandth (=milliseconds). 这是时间戳:HH:MM:SS:TTT,T代表千分之一(=毫秒)。 The milliseconds are important because the step size is small. 毫秒很重要,因为步长很小。 How to convert this data (cell) to data Matlab can handle (double)? 如何将该数据(单元格)转换为Matlab可以处理的数据(双精度)? So the data will be as follow: 因此数据如下:

0.515
0.671
0.828
0.984
1.140
etc.

The minutes and hours should be converted to seconds so it is easy to calculate the total time of the data or the average stepsize. 分钟和小时应转换为秒,以便轻松计算数据的总时间或平均步长。 So: 所以:

01.30.00.000    

will be: 将会:

5400.000

seconds

Thanks! 谢谢!

Assuming your cell array contains strings: 假设您的单元格数组包含字符串:

ts = {'00.00.00.515'
      '00.00.00.671'
      '00.00.00.828'
      '00.00.00.984'
      '00.00.01.140'};

it can be done as follows: 可以按照以下步骤完成:

>> cell2mat(cellfun(@(c) sscanf(c,'%d.%d.%f').', ts, 'uniformout', 0)) * [3600 60 1].'
ans =
    0.5150
    0.6710
    0.8280
    0.9840
    1.1400

This uses cellfun to convert each cell into a row vector of numbers with sscanf ,; 这使用cellfun将每个单元格转换为带有sscanf的数字行向量; then concatenates all those vectos into a matrix with cell2mat ; 然后将所有这些vectos连接到带有cell2mat的矩阵中; and fianlly applies matrix multiplication to compute time. 最后应用矩阵乘法来计算时间。

The code also works if the number of digits is not fixed. 如果位数不是固定的,该代码也可以使用。 For example: 例如:

>> ts = {'0.1.00.51'};
>> cell2mat(cellfun(@(c) sscanf(c,'%d.%d.%f').', ts, 'uniformout', 0)) * [3600 60 1].'
ans =
   60.5100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM