Timestamp in table not recognized as time value

Hello,
I have the following data time format in a table
'2020-08-03T20:40:00.000Z'
When I try to plot this data with another variable. I get the following message:
Error using scatter (line 54)
Input arguments must be numeric, datetime, duration or categorical.
I am aware that one can remove the T and the Z, in the following manner:
wtg_ref.Timestamp = strrep(wtg_ref.Timestamp, 'T', ' ');
wtg_ref.Timestamp = strrep(wtg_ref.Timestamp, 'Z', '');
However, that does and would not help either. It seems the value in the cell is not treated as time. A clue might be that the time value has quotation marks around it in the cells...
It would, of course be easier, to keep the T and Z in the format.
Support would be very much appreciated. Thank you.

1 件のコメント

Malik
Malik 2022 年 1 月 31 日
I should mention, I also tried: time = datenum('2020-08-01T04:26:18.887Z','yyyy-mm-ddTHH:MM:SS.FFFZ');
But then I lose the milliseconds when I datestr(result)

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

 採用された回答

Star Strider
Star Strider 2022 年 1 月 31 日

2 投票

It likel;y needs to be converted to a datetime array —
timestamp = '2020-08-03T20:40:00.000Z'
timestamp = '2020-08-03T20:40:00.000Z'
datetimestamp = datetime(timestamp, 'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''', 'TimeZone','UTC')
datetimestamp = datetime
03-Aug-2020 20:40:00
The desired 'Format' and time zone in the code can be whatever you want.
datetimestamp.Format = 'yyyy-MMM-dd HH:mm:ss.SSS'
datetimestamp = datetime
2020-Aug-03 20:40:00.000
datetimestamp.TimeZone = 'UTC-7'
datetimestamp = datetime
2020-Aug-03 13:40:00.000
The actual format of the month and day are a bit ambiguous, so change those if they are not as I defined them in 'InputFormat'.
.

6 件のコメント

Malik
Malik 2022 年 1 月 31 日
編集済み: Malik 2022 年 1 月 31 日
Hey Star Rider,
Thanks for coming to the rescue :-)
I am sorry, I can't manage to get the milliseconds with the above - It is probably me not following...
I have now tried the following as workaround:
old = '2020-08-03T20:40:00.111Z';
new = strrep(old, 'T', ' ');
new = strrep(new, 'Z', '')
time = datetime(new,'Format','yyyy-MM-dd HH:mm:ss.SSS')
And that gives the desired milliseconds
time = 2020-08-03 20:40:00.111
I would rather do it a more 'correct' or 'efficient' manner. If you could please advice.
Thanking you in anticipation.
Star Strider
Star Strider 2022 年 1 月 31 日
I do not understand the problem you are having with my code.
Please do not use the strrep calls, since they are not necessary. The 'InputFormat' string I use in my code correctly interprets the ‘T’ and ‘Z’ and produces the correct conversion.
A one-line version of my code, adding a new value to ‘old’ to demonstrate that it works with column vectors:
old = {'2020-08-03T20:40:00.111Z'; '2020-08-03T20:40:01.123Z'};
time = datetime(old, 'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''', 'TimeZone','UTC', 'Format','yyyy-MMM-dd HH:mm:ss.SSS')
time = 2×1 datetime array
2020-Aug-03 20:40:00.111 2020-Aug-03 20:40:01.123
It produces thae appropriate datetime values. (That does everything except the time zone conversion to a different time zone that I originally posted, and that is not absolutely necessary.)
.
Malik
Malik 2022 年 2 月 6 日
編集済み: Malik 2022 年 2 月 6 日
Thank you Star Strider! You a star!
Apologies, for the delay in response to get back to this. Was fixing another bit of code!
Star Strider
Star Strider 2022 年 2 月 6 日
As always, my pleasure! Thank you!
No worries! We all have lives outside of MATLAB Answers!
Xiaoqi Davey
Xiaoqi Davey 2023 年 2 月 17 日
Star Strider is the man!
Star Strider
Star Strider 2023 年 2 月 17 日
@Xiaoqi Davey — Thank you!!
A Vote would be appreciated!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTime Series Events についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 1 月 31 日

コメント済み:

2023 年 2 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by