datetime function vertical line

18 ビュー (過去 30 日間)
Lieke Numan
Lieke Numan 2019 年 2 月 5 日
コメント済み: Lieke Numan 2019 年 2 月 6 日
I have a plot where I use a datetime vector for x-axis (see example below). Now I want to make a vertical line within this plot, but when i use this:
x=17-Aug-2017 09:37:00
line([x x],[0 50])
I get this error:
Error using matlab.graphics.axis.decorator.DatetimeRuler/makeNumeric
Cannot combine or compare a datetime array with a time zone with one without a time zone.
Error in matlab.graphics.internal.makeNumeric
Example of first three samples in my datetime vector of my originally plotted data:
15-Aug-2017 17:58:58
15-Aug-2017 18:00:58
15-Aug-2017 18:02:58

採用された回答

Steven Lord
Steven Lord 2019 年 2 月 6 日
From the error message, you've plotted some datetime data that includes time zone information. If you do that, and you want to use line, xline, or yline you need to specify a datetime that includes time zone information in that *line call. Let's make two datetime objects, one with a time zone and one without.
withTZ = datetime('today', 'TimeZone', 'America/New_York')
withoutTZ = datetime('today')
Now let's make some data to plot.
v = 0:5;
x = withTZ + days(v);
plot(x, v.^2);
If we were to try to make an xline using withoutTZ, it would fail. But if we make one using withTZ, it works.
xline(withTZ + days(2.5)); % Works
xline(withoutTZ + days(1.5)); % Fails
  1 件のコメント
Lieke Numan
Lieke Numan 2019 年 2 月 6 日
Thanks a lot!!

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

その他の回答 (2 件)

Adam Danz
Adam Danz 2019 年 2 月 5 日
You're probably not using datetime formated variables. This works:
dt = datetime({'15-Aug-2017 17:58:58'
'15-Aug-2017 18:00:58'
'15-Aug-2017 18:02:58'});
figure
plot(dt, [40 45 50], '-o')
x=datetime('17-Aug-2017 09:37:00') % <-- make sure it's actually 'datetime'
line([x,x],[0 50])
  1 件のコメント
Sean de Wolski
Sean de Wolski 2019 年 2 月 5 日
In newer releases:
xline(x)

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


Lieke Numan
Lieke Numan 2019 年 2 月 6 日
編集済み: Lieke Numan 2019 年 2 月 6 日
Thanks for helping. Didn't know the xline function yet!
I tried both, and in both cases I'll get the same error, I have a datetime vector, so I expected it to work, but it didn't.
Error using matlab.graphics.axis.decorator.DatetimeRuler/makeNumeric
Cannot combine or compare a datetime array with a time zone with one without a time zone.
Error in matlab.graphics.internal.makeNumeric

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by