How to change HH:mm:ss:SSS to HH:mm:ss.SSS for a column?

43 views (last 30 days)
Hi, I'm collecting a data .txt file with multiple columns. One column is time but the issue is that the software I use exports the time data as HH:mm:ss:SSS (Note the last semicolon should be a period) and theres around 70,000 data points. I'm using readtable to create graphs. My question is: How to convert each data point of time in the column from HH:mm:ss:SSS to HH:mm:ss.SSS? Thank you for your help and insight.
  2 Comments
Walter Roberson
Walter Roberson on 20 Apr 2023
Could you confirm that the data is currently read in as cell array of character vectors stored in the table?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Apr 2023
Moved: Walter Roberson on 21 Apr 2023
time = {'01:18:34:754'; '13:04:19:999'}
time = 2×1 cell array
{'01:18:34:754'} {'13:04:19:999'}
converted = duration(string(datetime(time, 'InputFormat', 'HH:mm:ss:SSS', 'Format', 'HH:mm:ss.SSS')), 'format', 'hh:mm:ss.SSS')
converted = 2×1 duration array
01:18:34.754 13:04:19.999
  2 Comments
Walter Roberson
Walter Roberson on 21 Apr 2023
Unfortunately, duration() will only accept a small number of InputFormat, so the conversion cannot be direct.
Walter Roberson
Walter Roberson on 21 Apr 2023
time = {'01:18:34:754'; '13:04:19:999'}
time = 2×1 cell array
{'01:18:34:754'} {'13:04:19:999'}
temp = char(time);
temp(:,end-3) = '.';
converted = duration(cellstr(temp), 'Format', 'hh:mm:ss.SSS')
converted = 2×1 duration array
01:18:34.754 13:04:19.999

Sign in to comment.

More Answers (2)

chicken vector
chicken vector on 20 Apr 2023
Edited: chicken vector on 20 Apr 2023
if the time format is imported from the .txt as a string, you can do the following using a for loop for every element:
time = '01:18:34:754';
semicolons = strfind(time, ':');
time(semicolons(end)) = '.'
time =
'01:18:34.754'
To speed things up, since the format is standardsed, you can also do directly:
time(9) = '.';
Otherwise you need to specify what format are you working with.
Also I suggest you to have a look at duration which you may find helpful.
  2 Comments
Walter Roberson
Walter Roberson on 21 Apr 2023
This needs a bit of modification for entries coming from a table, as those would either be a cell array of character vectors (more likely) or a string array (only if specifically configured for.)
chicken vector
chicken vector on 21 Apr 2023
Edited: chicken vector on 21 Apr 2023
You are right, I was trying not to speculate too much given the little information provided, but assuming a cell array is definetly the right choice.

Sign in to comment.


Stephen23
Stephen23 on 21 Apr 2023
Edited: Stephen23 on 21 Apr 2023
time = {'01:18:34:754'; '13:04:19:999'};
data = rand(2,1);
T = table(time,data)
T = 2×2 table
time data ________________ _______ {'01:18:34:754'} 0.73654 {'13:04:19:999'} 0.68067
tmp = regexprep(T.time,':(?=\d{3})','.');
T.time = duration(tmp, 'Format','hh:mm:ss.SSS')
T = 2×2 table
time data ____________ _______ 01:18:34.754 0.73654 13:04:19.999 0.68067

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!