Set specific date ticks

Hi trying to set specific date ticks. I don't understand why the following code does not work
figure()
hold on
box on
plot(date1,series1,'k')
plot(date1,series2,'r--')
xlabel('Date')
ylabel('Price')
DateString = {'06/30/2010';'11/30/2010'};
datenum(DateString);
set(gca, 'XTick', datenum(DateString))
dateFormat = 1;
datetick('x', dateFormat)

 Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Oct. de 2017

1 voto

datetick('x', dateFormat, 'keepticks')

5 comentarios

EM
EM el 13 de Oct. de 2017
Hi Walter, are you implying that the only adjustment I need is to include 'keepticks' ?
Walter Roberson
Walter Roberson el 13 de Oct. de 2017
Yes. datetick() normally ignores any tick positions already set and calculates "nice" tics based upon the time format. So use the 'keepticks' option.
Setting the tick positions after calling datetick() would not work: datetick() sets the tick labels to strings, and those strings would not be reflect the positions you set afterwards.
plotting with datetime objects is even better; datetick was always a bit of a kludge.
EM
EM el 13 de Oct. de 2017
Thank you!
EM
EM el 21 de Oct. de 2017
Hi I'm still receiving an error. Can you help?
Here is the code:
figure()
hold on
box on
plot(date1,series1,'k')
plot(date1,series2,'r--')
xlabel('Date')
ylabel('Price')
DateString = {'06/30/2010';'11/30/2010'};
datenum(DateString);
set(gca, 'XTick', datenum(DateString))
dateFormat = 1;
datetick('x', dateFormat,'keepticks')
Here is the error:
Error using matlab.graphics.axis.decorator.DatetimeRuler/validateTicks Value must be a vector of increasing datetime values.
Error in matlab.graphics.axis.decorator.DatetimeRuler/setTicksDelegate
Walter Roberson
Walter Roberson el 21 de Oct. de 2017
Your date1 appears to be datetime objects, and you appear to be using R2016b or later. In that case you do not use datetick.
figure()
hold on
box on
plot(date1, series1, 'k')
plot(date1, series2, 'r--')
xlabel('Date')
ylabel('Price')
DateString = {'06/30/2010';'11/30/2010'};
ticlocs = datetime(DateString); %R2014b or later
ticlocs.Format = 'dd-MMM-yyyy';
set(gca, 'XTick', ticklocs); %R2016b or later

Iniciar sesión para comentar.

Más respuestas (2)

Peter Perkins
Peter Perkins el 13 de Oct. de 2017
Editada: Walter Roberson el 13 de Oct. de 2017

3 votos

If you have access to R2016b or later, you will probably be much happier using datetime rather than datenum/datestr, including for plotting:
d = datetime(2015,1,1:500);
x1 = randn(size(d));
plot(d,x1)
ax = gca;
ax.XAxis.TickValues = datetime(2015,1,1)+calquarters(0:6);
ax.XAxis.TickLabelFormat = 'QQQ-yyyy';

3 comentarios

EM
EM el 13 de Oct. de 2017
Can you use this approach to place ticks at specific dates? For example I have 8 specific dates that I'd like to place a tick at. So I cannot simply use a start date and increase it by quarters.
Walter Roberson
Walter Roberson el 13 de Oct. de 2017
Yup. For example,
ax.XAxis.TickValues = datetime(2015,1,1)+days([18 143 207 258]);
ax.XAxis.TickLabelFormat = 'MMM dd';
or
ax.XAxis.TickValues = datetime(2015,[1;5;11;14], [17;5;30;19]);
ax.XAxis.TickLabelFormat = 'yy/M/d';
EM
EM el 13 de Oct. de 2017
Thank you Walter, that is very helpful.

Iniciar sesión para comentar.

Med Aymane Ahajjam
Med Aymane Ahajjam el 14 de Mayo de 2019

0 votos

In order for the TickLabelFormat to work, at least in my case, I had to specify:
ax.XTickLabelMode = 'manual';
ax.XAxis.TickLabelFormat = 'HH:mm:ss';

Productos

Preguntada:

EM
el 13 de Oct. de 2017

Respondida:

el 14 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by