why does this not work?

조회 수: 7 (최근 30일)
Rob
Rob 2025년 3월 27일
댓글: Walter Roberson 2025년 3월 27일
>> x='0:07.50 2:03.91 3:57.36 5:09.44 6:32.90 7:43.03 9:43.45';
>> datetime(x,'InputFormat','m:ss.SS')
Error using datetime (line 257)
Unable to convert '0:07.50 2:03.91 3:57.36 5:09.44 6:32.90 7:43.03 9:43.45' to
datetime using the format 'm:ss.SS'.

채택된 답변

Les Beckham
Les Beckham 2025년 3월 27일
편집: Les Beckham 2025년 3월 27일
That doesn't work because your x is one long character vector and datetime tries to match your input format to the entire contents of x. Instead define x as either a string array or a cell array of character vectors.
Using a string array:
x = [ "0:07.50" "2:03.91" "3:57.36" "5:09.44" "6:32.90" "7:43.03" "9:43.45" ];
dt1 = datetime(x,'InputFormat','m:ss.SS')
dt1 = 1x7 datetime array
27-Mar-2025 00:00:07 27-Mar-2025 00:02:03 27-Mar-2025 00:03:57 27-Mar-2025 00:05:09 27-Mar-2025 00:06:32 27-Mar-2025 00:07:43 27-Mar-2025 00:09:43
Using a cell array of char vectors:
x= { '0:07.50' '2:03.91' '3:57.36' '5:09.44' '6:32.90' '7:43.03' '9:43.45' };
dt2 = datetime(x,'InputFormat','m:ss.SS')
dt2 = 1x7 datetime array
27-Mar-2025 00:00:07 27-Mar-2025 00:02:03 27-Mar-2025 00:03:57 27-Mar-2025 00:05:09 27-Mar-2025 00:06:32 27-Mar-2025 00:07:43 27-Mar-2025 00:09:43
isequal(dt1, dt2)
ans = logical
1
Edit:
If you want to convert your datetime array to numeric seconds:
dt_seconds = 60*minute(dt1) + second(dt1)
dt_seconds = 1×7
7.5000 123.9100 237.3600 309.4400 392.9000 463.0300 583.4500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

추가 답변 (3개)

John D'Errico
John D'Errico 2025년 3월 27일
It failed because datetime did not recognize that string as a list of distinct times. Just because you know what you want code to do, does not mean it will read your mind. Code is pretty dumb in general.
x='0:07.50 2:03.91 3:57.36 5:09.44 6:32.90 7:43.03 9:43.45';
xspl = strsplit(x,' ')
xspl = 1x7 cell array
{'0:07.50'} {'2:03.91'} {'3:57.36'} {'5:09.44'} {'6:32.90'} {'7:43.03'} {'9:43.45'}
datetime(xspl,'InputFormat','m:ss.SS')
ans = 1x7 datetime array
27-Mar-2025 00:00:07 27-Mar-2025 00:02:03 27-Mar-2025 00:03:57 27-Mar-2025 00:05:09 27-Mar-2025 00:06:32 27-Mar-2025 00:07:43 27-Mar-2025 00:09:43

Star Strider
Star Strider 2025년 3월 27일
편집: Star Strider 2025년 3월 27일
need to separate the elements of the array (that I changed to a cell array here).
Then, it works —
x = {'0:07.50' '2:03.91' '3:57.36' '5:09.44' '6:32.90' '7:43.03' '9:43.45'};
datetime(x,'InputFormat','m:ss.SS')
ans = 1x7 datetime array
27-Mar-2025 00:00:07 27-Mar-2025 00:02:03 27-Mar-2025 00:03:57 27-Mar-2025 00:05:09 27-Mar-2025 00:06:32 27-Mar-2025 00:07:43 27-Mar-2025 00:09:43
.
EDIT — (27 Mar 2025 at 21:13)
Use timeofday with seconds.
Perhaps this —
x = {'0:07.50' '2:03.91' '3:57.36' '5:09.44' '6:32.90' '7:43.03' '9:43.45'};
dt = datetime(x,'InputFormat','m:ss.SS')
dt = 1x7 datetime array
27-Mar-2025 00:00:07 27-Mar-2025 00:02:03 27-Mar-2025 00:03:57 27-Mar-2025 00:05:09 27-Mar-2025 00:06:32 27-Mar-2025 00:07:43 27-Mar-2025 00:09:43
dtm = seconds(timeofday(dt))
dtm = 1×7
7.5000 123.9100 237.3600 309.4400 392.9000 463.0300 583.4500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This works, and there may also be other ways of doing that conversion.
.

Rob
Rob 2025년 3월 27일
편집: Rob 2025년 3월 27일
I appreciate all the answers, and apologize for not just saying what I want to happen. I want to translate a string array
[ "0:07.50" "2:03.91" "3:57.36"]
into numbers
[0*60 + 7.5, 2*60 + 3.91, 3*60 + 57.36]
ans =
7.5000 123.9100 237.3600
For 3 elements in the string array, doing it manually is fine. But I have dozens.
  댓글 수: 3
Steven Lord
Steven Lord 2025년 3월 27일
Or you can create a duration instead of a datetime and avoid having to do the dateshift.
S = [ "0:07.50" "2:03.91" "3:57.36"];
DT = duration(S, "InputFormat", "mm:ss.SS")
DT = 1x3 duration array
00:00:07 00:02:03 00:03:57
D = seconds(DT)
D = 1×3
7.5000 123.9100 237.3600
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Walter Roberson
Walter Roberson 2025년 3월 27일
Ah, when I tested with duration, I missed that you need to use mm instead of m

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

카테고리

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

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by