hh:mm:ss string to duration type
13 次查看(过去 30 天)
显示 更早的评论
I have strings that look like this: '24:06:26' and '7:20:26'. I want a duration type. What is the best way to get this?
All I have right now is
str = ' 7:20:26' % or '24:06:26'
d = duration(cellfun(@(x) str2double(x), regexp(str,'\d{1,2}','match')));
This seems inefficient and overkill because I have to use at least 3 function calls to get to a duration type.
0 个评论
采纳的回答
Guillaume
2016-1-11
You can get rid of the cellfun call, str2double works directly on cell arrays. The other two functions are necessary in any case. datetime just hides the string splitting and string to number conversion in its constructor.
You could use strsplit instead of regexp. I'm not sure which would be more efficient:
d = duration(str2double(strsplit(str, ':')));
更多回答(0 个)
另请参阅
类别
在 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!