Setting plot XAxis limits using a segment of time in the data

조회 수: 21 (최근 30일)
Matt Martin
Matt Martin 2017년 5월 11일
댓글: Matt Martin 2017년 5월 12일
Apologies if this sounds very elementary but I am plotting a line plot where the x axis is time. My variable (dtg) is in datetime format but goes for 3 consecutive days. I would like to plot only the range 10 Apr 2017 16:00:00 to 10 Apr 2017 18:20:00. All my other axis properties are fine, but xlim keeps giving me a error saying that Apr is not defined. I am using R2016b
ax=gca; ax.xlim([10 Apr 2017 16:00:00,10 Apr 2017 18:20:00]);
Thanks

답변 (1개)

Jan
Jan 2017년 5월 11일
t1 = datenum('10 Apr 2017 16:00:00');
t2 = datenum('10 Apr 2017 18:20:00');
ax.xlim([t1, t2]);
Does this work? If not, read the doc datenum to adjust the time format.
  댓글 수: 1
Matt Martin
Matt Martin 2017년 5월 12일
Thanks Jan, You got me in the right direction and I got there quickly after your suggestion. Final looks like this:
t1 = string({'12 Apr 2017 16:00:00'});
t2 = string({'12 Apr 2017 18:20:00'});
t1 = datetime(t1, 'Format', 'dd MMM yyy HH:mm:ss', 'TimeZone', 'UTC');
t2 = datetime(t2, 'Format', 'dd MMM yyy HH:mm:ss', 'TimeZone', 'UTC');
xlim([t1,t2]);
Worked great! Again, thanks for the push in the right direction.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by