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

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)

 Risposta accettata

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 Commenti

@Star Strider But it doesn't work with my plot!
Try this —
LD = load('Pul GIULIA_MMEQ1.mat');
GIULIAMMEQ1 = LD.GIULIAMMEQ1;
% First10Rows = GIULIAMMEQ1(1:10,:); % Information
GIULIAMMEQ1.Dec1997 = datetime(GIULIAMMEQ1.Dec1997, 'InputFormat','dd MMM yyyy'''';');
% First10Rows2 = GIULIAMMEQ1(1:10,:); % Information
NEW = str2double(GIULIAMMEQ1.Var4) * 10 * 0.35;
newTicksc = {'2000','2002','2004','2006','2008','2010','2012','2014','2016','2018'}; % 'cell' Array
newTicks = datetime(newTicksc,'InputFormat','yyyy', 'Format','yyyy'); % 'datetime' Array
figure
plot(GIULIAMMEQ1.Dec1997, NEW)
grid
xt = xticks;
newXTick = linspace(str2double(newTicksc{1}), str2double(newTicksc{end}), numel(newTicks));
set(gca, 'XTick',newTicks)
I cannot run that using the online Run feature because .mat files are too difficult to use with it. It works offlilne.
.
It worked. Thank you!
As always, my pleasure!
.

Accedi per commentare.

Più risposte (1)

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