MATLAB: How to format x date axis

format date x axis

Hi
I did try to find a answer online but I didn't get a good one so I am asking here for clearer help.
In my plot (z vs. date) with date(mmm-dd-yyyy) being x axis. I'd like to format x-axis so it start from Jan-01-2008 to Jan-01-2014 with an interval of every 90 days(3 months). I never get correct answers. I am very confusing by the results.
Here are what I did.
1. convert date from excel to datenum, dn_Prod=datenum(CycleDate_Prod,'mm/dd/yyyy');
2. plot the line, hLine=plot(hAx(1),dn_Prod,equivalentheave,'-k','linewidth',2,'DisplayName',legendlabel_equiuplift);
3. format the x-axis
datetick('x','mmm-YYYY','keepticks','keeplimits');
startdatenum=datenum('Jan-01-2008','mmm-dd-yyyy');
enddatenum=datenum('Jan-01-2014','mmm-dd-yyyy');
set(gca,'XTick',[startdatenum:90:enddatenum]);
I think I did correctly, but I never get it right.
Anyone help me find the problem in the above scripts? Thanks a lot.

Best Answer

If you're using a sufficiently recent release of MATLAB, release R2016b or later, you can call plot with a datetime array as one of the inputs. I recommend doing that instead of calling plot with a vector of serial date numbers generated by datenum. For more information see this example in the documentation.