필터 지우기
필터 지우기

How to convert string to time?

조회 수: 3 (최근 30일)
Liqing
Liqing 2011년 3월 22일
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.

채택된 답변

Paulo Silva
Paulo Silva 2011년 3월 22일
str2num(strrep('04:55',':',''))
  댓글 수: 1
Liqing
Liqing 2011년 3월 22일
Thank you very much. This is a very smart way of handling this.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt Tearle
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.)
  댓글 수: 1
Liqing
Liqing 2011년 3월 22일
It's very nice to see the use of regexp to split the string into two values.
This could be a even better solution.
Thank you very much.

댓글을 달려면 로그인하십시오.


Walter Roberson
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').';
  댓글 수: 1
Liqing
Liqing 2011년 3월 22일
Thank you very much for the reply. This is another way of achieving it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by