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

2 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by