简体   繁体   中英

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

My data (cell) in Matlab looks like this:

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). The milliseconds are important because the step size is small. How to convert this data (cell) to data Matlab can handle (double)? 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 ,; then concatenates all those vectos into a matrix with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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