adding year to plot using datetick

Asked by DoVile Last Name: on 11 Jul 2012
Latest activity Commented on by Kelly Kearney on 11 Jul 2012

I have created a plot for a volatility measure, now i want to add the years of the measure to the x axis. I have the appropriate year in a .txt file which i have loaded into the variable 'dates'. However i can not get the labels onto my plot

plot(1:iNumObs,exp(vLogVolsBF),'r:')
xlabel('Trading days since 2000-01-03')
ylabel('Volatility')
title('Particle filter estimate of volatility');
datetick('dates','yyyy');

It returns the error:

??? Error using ==> datetick>parseinputs at 344 The axis must be 'x','y', or 'z'.

Error in ==> datetick at 109 [axh,nin,ax,dateform,keep_ticks,keep_limits] = parseinputs(varargin);

Thank you

0 Comments

DoVile Last Name:

Tags

Products

No products are associated with this question.

2 Answers

Answer by DoVile Last Name: on 11 Jul 2012
Accepted answer

Thank you for your answer, it is the second case that is relevant. My data consists of 3081 observations, having loaded the year for each observation into the variable 'dates' i thought i was trying to do exactly as you suggest using datetick('dates','yyyy'); after my plot ?

3 Comments

John Petersen on 11 Jul 2012

'x' refers to the axis, not the variable x. I don't see any variable called 'dates' defined in your question.

DoVile Last Name: on 11 Jul 2012

ahh that makes sense, but then how does the function know where i have stored the dates ?

I the original question i wrote: "I have the appropriate year in a .txt file which i have loaded into the variable 'dates'". To be perfectly clear, in the variable dates i have a list of years as long as the list of observations and indicating which year these observations are from.

Kelly Kearney on 11 Jul 2012

The datetick function assumes that the values on your x-axis are datenumbers. So, in your case, rather than using 1:Nobs as your x values when calling plot, you should use

 plot(dates, exp(vLogVolsBF),'r:');
 datetick('x', 'yyyy');

This is assuming that the "dates" variable actually holds datenumbers (in the range of 730000 or so), rather than year numbers. Look at the help for datenum to get a better understanding of this.

DoVile Last Name:
Answer by John Petersen on 11 Jul 2012
Edited by John Petersen on 11 Jul 2012

You have 1:iNumObs as the x-axis. I assume this is where you want the years. If your data is on a yearly basis, just add the first year (-1) to the x-axis

plot(1999+[1:iNumObs], exp(vLogVolsBF),'r:')

If your data is throughout the year but you just want a label for each year, then you need the date of each data point. If it's in datenum for example, then you can use datetick to convert to the format you're looking for,

datetick('x','yyyy')

0 Comments

John Petersen

Contact us