Adapting the datetime function

18 ビュー (過去 30 日間)
jrbbrt
jrbbrt 2018 年 7 月 13 日
編集済み: Steven Lord 2018 年 7 月 13 日
Hi everybody!
I got: a string representing a time in the format of 'dd.MM.yyyy HH:MM:SS.SSS' (actually not only one string, but a whole list)
I'd like to: transform my string into a datetime with the help of the function "datetime"
That's what I tried so far:
With the following line of code I'm always getting the error message "Error using datetime (line 639) Input data must be a numeric array, a string array, a cell array containing character vectors, or a char matrix."
date=datetime(t.r(:,1),'Inputformat','dd.MM.yyyy HH:MM:SS.SSS');
Unfortunately: I don't know how to adapt the datetime function properly! Can you please help me?

採用された回答

Paolo
Paolo 2018 年 7 月 13 日
編集済み: Paolo 2018 年 7 月 13 日
You must use the correctly specifiers for 'InputFormat', mm for minutes and ss for seconds.
str = "13.07.2018 09:59:30.333"
datetime(str,'InputFormat','dd.MM.yyyy HH:mm:ss.SSS')
  2 件のコメント
jrbbrt
jrbbrt 2018 年 7 月 13 日
So basically my string looks like
"13.07.2018 10:00:22.333"
and though I adapt everything like you said, I unfortunately still get the same error message. Do you have an idea why? Or how to work around this?
Paolo
Paolo 2018 年 7 月 13 日
Apologies, I missed that the milliseconds are preceded by a full stop and not a colon.
Try:
datetime(str,'InputFormat','dd.MM.yyyy HH:mm:ss.SSS')

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2018 年 7 月 13 日
編集済み: Steven Lord 2018 年 7 月 13 日
Define a variable and show us the class and size of that variable. You can show both using the whos function.
z = t.r(:,1);
whos z
The error message states:
"Error using datetime (line 639) Input data must be a numeric array, a string array, a cell array containing character vectors, or a char matrix."
To satisfy the requirement to be valid input data to the datetime function, z must be one of a numeric array (check with isnumeric), a string array (check with isstring), a cell array containing character vectors (check with iscell and iscellstr), or a char matrix (check with ischar and ismatrix).
From your description my guess is that you have a cell array containing a string array.
>> D = datetime({"13.07.2018 09:59:30.333"})
Error using datetime (line 639)
Input data must be a numeric array, a string array, a cell array containing
character vectors, or a char matrix.
If that's the case, you may want to switch to using just a plain string array.
>> D = datetime(["13.07.2018 09:59:30.333"; "10.08.2018 13:15:47.123"], ...
'InputFormat', 'dd.MM.yyyy HH:mm:ss.SSS')

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by