How to convert string to time?
3 次查看(过去 30 天)
显示 更早的评论
Sorry for the silly question, but could anyone show me how to convert a string, such as, '04:55' to numerical values of hour and minute?
Thank you very much.
0 个评论
采纳的回答
更多回答(2 个)
Matt Tearle
2011-3-22
If you want an actual decimal minute value:
x = '04:55'
y = str2double(regexp(x,':','split'))
z = y*[1;1/60]
(Leave off the last line if you just want minutes and seconds as separate array elements.)
Walter Roberson
2011-3-22
Is the string fixed width, with a leading 0? If so then,
H = T(1) - '0' * 10 + T(2) - '0';
M = T(4) - '0' * 10 + T(5) - '0';
Or
H = str2num(T(1:2));
M = str2num(T(4:5));
Or
H = [10 1] * (T(1:2)-'0').';
M = [10 1] * (T(4:5)-'0').';
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!