Datevec problem - not able to convert string to numeric array

1 回表示 (過去 30 日間)
Orkun OZENER
Orkun OZENER 2017 年 10 月 27 日
回答済み: Peter Perkins 2017 年 11 月 16 日
Hi I have problem with datevec. I have vector like this. I am trying to parse data for obtaining second. But it always gives an error for me. I have my time vector in String Format in 2017a.
"2017-10-16T14:39:00.221Z"
"2017-10-16T14:39:02.221Z"
"2017-10-16T14:39:03.221Z"
"2017-10-16T14:39:04.221Z"
"2017-10-16T14:39:06.221Z"
"2017-10-16T14:39:08.221Z"
"2017-10-16T14:39:09.221Z"
"2017-10-16T14:39:10.221Z"
"2017-10-16T14:39:11.221Z"
The code that I write is
Time= strrep(Time, 'Z', ''); %find .XXXXZ replace to''
Time= strrep(Time, 'T', ' '); %find T replace to' '
formatIn = 'yyyy-mm-dd HH:MM:SS'
datevec(Time,formatIn)
The answer of matlab is
formatIn =
'yyyy-mm-dd HH:MM:SS'
Error using datevec (line 103) The input to DATEVEC was not an array of character vectors. What Can I do???

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 27 日
編集済み: Andrei Bobrov 2017 年 10 月 27 日
A = {'2017-10-16T14:39:00.221Z'
'2017-10-16T14:39:02.221Z'
'2017-10-16T14:39:03.221Z'
'2017-10-16T14:39:04.221Z'
'2017-10-16T14:39:06.221Z'
'2017-10-16T14:39:08.221Z'
'2017-10-16T14:39:09.221Z'
'2017-10-16T14:39:10.221Z'
'2017-10-16T14:39:11.221Z'};
Time = strung(A);
dt = datetime(regexprep(A,{'T','Z'},{' ',''}),'I','uuuu-MM-dd HH:mm:ss.SSS');
out = datevec(dt);
  2 件のコメント
Stephen23
Stephen23 2017 年 10 月 27 日
編集済み: Stephen23 2017 年 10 月 27 日
datetime accepts literals, so regexprep is not required. Perhaps something like this (untested):
'uuuu-MM-dd''T''HH:mm:ss.SSS''Z'''
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 27 日
Thank you Stephen!

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

その他の回答 (2 件)

Orkun OZENER
Orkun OZENER 2017 年 10 月 27 日
Thank you for your answer It works. But what is I is doing at there..I did not find the meaning of it..Is it a format?
dt = datetime(regexprep(A,{'T','Z'},{' ',''}),'I','uuuu-MM-dd HH:mm:ss.SSS');
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 27 日
"I did not find the meaning of it..Is it a format?"
Did you read the datetime help? It explains what that is.

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


Peter Perkins
Peter Perkins 2017 年 11 月 16 日
Stephen's mod of Andrei's answer looks right, but I would add that there is likely no need to call datevec. In other words, you probably don't want to convert text to a numeric array, you want to convert it to datetimes.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by