MATLAB: Datetick at even monthly intervals

10,568

Solution 1

Before converting your serial date numbers to dates you will need to set your axes ticks.

I just plotted some random numbers vs date as an example:

date1 = datenum([2012 01 01 00 00 00]);
date2 = now;
dateV = date1:date2; % x-axis data
y = rand(length(dateV),1); % y-axis data
hl = plot(dateV,y);
hax = get(hl, 'Parent'); % axes handle

By setting XTick property of the axes like this, ticks will be placed for every 30th element of dateV. You can change this to suit your tick intervals.

set(hax, 'XTick', [dateV(1:30:end)]); 
datetick('x', 24, 'keepticks'); 24 is a date format identifier. You can select a format from the datetick documentation.

As the datetick documentation points out, you need to set the axis ticks before you run the datetick function.

Solution 2

Do you know datetick? For your example:

% date (x-data)
years = [2008 2008 2009 2009 2010 2010 2011];
months = [1 7 1 7 1 7 1];

% do plot (NOTE: datenum is vectorized!)
plot( datenum(years, months,1),  [your y-data] )

% set axis labels
datetick('x','mm-yyyy')

Or, a bit more general,

% generate periodic data
[years, months] = meshgrid(2008:2011, 1:12); % or whatever range you want

% do plot and set axes
plot( datenum(years(:), months(:), 1),  [your y-data] )
datetick('x','mm-yyyy')
Share:
10,568

Related videos on Youtube

janon128
Author by

janon128

Updated on September 15, 2022

Comments

  • janon128
    janon128 over 1 year

    I want to mark my time series at even monthly intervals from Jan 2008 to Jan 2011.

    EDIT: The time series already has a time component in datenum format:

    733408 x1
    733410 x2
    etc...
    

    I apologize for not making this clear in my original statement of the question.

    A straightforward twice-annually implementation becomes incredibly messy:

    years = [2008 2008 2009 2009 2010 2010 2011];
    months = [1 7 1 7 1 7 1];
    days = ones(1,7);
    for k = 1:7
        dates(k) = datenum(years(k), months(k), days(k));
    end
    labels = datestr(dates,'mmm-yy');
    set(gca,'XTick',dates);
    set(gca,'XTickLabel',labels);
    

    To expand this to monthly ticks:

    years = [2008 ...repeat10times 2009 ...repeat10times 2010 ...repeat10times 2011]
    m = 1:11; months = repmat(m,1,3)
    days = ones(1:34)
    

    There must be a simpler way!

  • janon128
    janon128 over 11 years
    All right I'll give that a try. Thanks. But to expand that to each-monthly ticks would mean repeating 2008 11 times, 2009 11 times, etc, in the years vector. Any simple way to make this vector?
  • Rody Oldenhuis
    Rody Oldenhuis over 11 years
    @janon128: try my second example. That's exactly what it does :)
  • janon128
    janon128 over 11 years
    It works for a time series with one data point per mark? I apologize for not making it clear in my original question that the time series I'm working with already has a time component in "datenum" format. 733408 (corresponding to Jan 1 2008)...etc all the way to Dec 31 2011.