How to set number of ticks along X axis in matlab?

9.5k views Asked by At

I'm having trouble setting the appropriate number of ticks along the Xaxis in Matlab. As you can see below, I set the number to 2 in ha.XTicksNumber=2, yet it still plots 10.

nSites = 2;

ha = tight_subplot(nSites,1,[.01 .01],[.1 .1],[.1 .1]);
display(ha);
for ii = 1:nSites; 
    axes(ha(ii));
    xData=linspace(1,100,90); 
    plot(xData);
    if ii~=nSites
        set(ha,'XTickLabel','');
    else
        set(ha,'XTickLabel','');
        ha.XTicksNumber=2;
        ha.XTick = linspace(1,90,55);
        datetick('x','mm/dd','keepticks');
    end 
end

The above code is reproducible. Thanks for any help!

What I want in the end is an x axis with dates, but I want to be able to either

1) set the number of dates along the x axis OR 2) set the interval between any two dates.

Anyone know how to do this?

1

There are 1 answers

1
sayan On BEST ANSWER

Try this out

NumTicks = 4;
L = get(gca,'XLim');
set(gca,'XTick',linspace(L(1),L(2),NumTicks))

You can easily wrap it in a function if you like.