Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
Pul
Pul am 9 Dez. 2021
Kommentiert: Star Strider am 12 Dez. 2021
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)

Akzeptierte Antwort

Star Strider
Star Strider am 9 Dez. 2021
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 Kommentare
Pul
Pul am 12 Dez. 2021
It worked. Thank you!
Star Strider
Star Strider am 12 Dez. 2021
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 9 Dez. 2021
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.

Community Treasure Hunt

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

Start Hunting!

Translated by