필터 지우기
필터 지우기

pre-set axis limit to datetime

조회 수: 28 (최근 30일)
Yu Li
Yu Li 2022년 2월 22일
댓글: Steven Lord 2023년 2월 19일
Hi:
I have a figure and I want to set the x-axis to 'datetime' type before add any plot there, below is the code:
figure
ax=gca;
ax.XLim=[datetime('now'),datetime('now')+day(1)]
Matlab reported error like below:
Value must be a 1x2 vector of numeric type in which the second element is greater than the first element or is Inf.
however, the code below works:
figure
ax=gca;
ax.XLim=[1,2]
and the code below works:
ax=gca;
ax.XLim=[1,2]
figure
plot([datetime('now'),datetime('now')+day(1)],[1,1])
so, it seems like the Matlab do not accept set the x-axis to datetime if no 'datetime' type plot added to the figure.
is there anyway can address this issue?
Thanks!
Yu

채택된 답변

Seth Furman
Seth Furman 2022년 2월 25일
The axis ruler can be set directly to a DatetimeRuler.
ax = axes;
ax.XAxis = matlab.graphics.axis.decorator.DatetimeRuler;
ax.XLim = [datetime(2020,1,1) datetime(2020,1,2)];
  댓글 수: 4
VBBV
VBBV 2023년 2월 19일
편집: VBBV 2023년 2월 19일
figure
ax=gca;
ax.XAxis = matlab.graphics.axis.decorator.DatetimeRuler;
plot(ax,[datetime('now','TimeZone', 'America/New_York'),datetime('now','TimeZone', 'America/New_York')+day(2)])
cla(ax)
ax.XLim = [datetime('now','TimeZone', 'America/New_York'),datetime('now','TimeZone', 'America/New_York')+day(2)]
Value must be a 1x2 vector of numeric type in which the second element is greater than the first element or is Inf.
ax.YLim = [ 0 1]
if cla(ax) is used as told, it still produces the same error
Steven Lord
Steven Lord 2023년 2월 19일
If you don't want a visible line on your axes you could plot using NaN as the Y data.
N = datetime('now','TimeZone', 'America/New_York');
plot(N, NaN);
xlim([N, N+days(2)])
ylim([0 1]);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by