How to change HH:mm:ss:SSS to HH:mm:ss.SSS for a column?
42 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2023-4-20
Could you confirm that the data is currently read in as cell array of character vectors stored in the table?
采纳的回答
Walter Roberson
2023-4-21
移动:Walter Roberson
2023-4-21
time = {'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')
2 个评论
Walter Roberson
2023-4-21
Unfortunately, duration() will only accept a small number of InputFormat, so the conversion cannot be direct.
Walter Roberson
2023-4-21
time = {'01:18:34:754'; '13:04:19:999'}
temp = char(time);
temp(:,end-3) = '.';
converted = duration(cellstr(temp), 'Format', 'hh:mm:ss.SSS')
更多回答(2 个)
chicken vector
2023-4-20
编辑:chicken vector
2023-4-20
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.
2 个评论
Walter Roberson
2023-4-21
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
2023-4-21
编辑:chicken vector
2023-4-21
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!