필터 지우기
필터 지우기

How can I see the right labels on x-axis?

조회 수: 4 (최근 30일)
Pul
Pul 2021년 12월 9일
댓글: Star Strider 2021년 12월 12일
Hello everyone,
why do I got this error and I can't see the right labels on my plot?
Can anyone help me please?
Thank you.
load('GIULIA_MMEQ1.mat');
A=GIULIAMMEQ1.Var4;
B=str2double(A);
NEW= B * 10 * 0.35;
C=GIULIAMMEQ1.Dec1997;%array2table
C=replace(C,"';","");
C=datetime(C,'InputFormat','dd MMM yyyy'); %convert to datetime format
newTicks = datetime({'2000','2002','2004','2006','2008','2010','2012','2014','2016','2018'},'InputFormat','u');
xticks([2000,2002,2004,2006,2008,2010,2012,2014,2016,2018]);
xticks(newTicks);
Error using xticks (line 33)
Tick values must be a vector of increasing numeric values.
xticklabels(['2000','2002','2004','2006','2008','2010','2012','2014','2016','2018']);
hold on
plot(C,NEW)

채택된 답변

Star Strider
Star Strider 2021년 12월 9일
Without downloading the .mat file and running the entire code, this seems to work —
newTicks = datetime({'2000','2002','2004','2006','2008','2010','2012','2014','2016','2018'},'InputFormat','yyyy', 'Format','yyyy');
figure
plot(newTicks, rand(size(newTicks)))
.
  댓글 수: 4
Pul
Pul 2021년 12월 12일
It worked. Thank you!
Star Strider
Star Strider 2021년 12월 12일
As always, my pleasure!
.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 12월 9일
You have not plotted anything to this point.
newTicks = datetime({'2000','2002','2004','2006','2008','2010','2012','2014','2016','2018'},'InputFormat','u');
You create a vector of datetime values, but nothing has been plotted yet
xticks([2000,2002,2004,2006,2008,2010,2012,2014,2016,2018]);
You tell xticks to use a series of floating point numbers. Because you have not plotted anything yet, this use of floating point numbers for the tick positions tells MATLAB that the x axes should be created as a numeric axes (not as a datetime or map or polar axes)
xticks(newTicks);
You have already created ticks, so this command would be to change the ticks to something else. But the previous xticks() call forced the axes to numeric, and this xticks() is trying to use datetime ticks. You cannot use datetime() ticks on a numeric axes.
... Just skip the xticks() giving the numeric values.

카테고리

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