How to format x date axis?

조회 수: 15 (최근 30일)
roudan
roudan 2018년 1월 11일
댓글: Walter Roberson 2018년 1월 11일
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.

채택된 답변

Steven Lord
Steven Lord 2018년 1월 11일
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.
  댓글 수: 2
roudan
roudan 2018년 1월 11일
Thanks Steven, I found a solution now. datetick() has to be after set() function then it works. don't know why. Thanks
Walter Roberson
Walter Roberson 2018년 1월 11일
datetick() sets the XTickLabels based upon the current tick positions (if 'keepticks' was specified) or based upon the data range (if 'keepticks' was not specified.) datetick() does not modify the properties of the axes to know that date format should be used for all changes to XTick that are made after datetick() is called.
The R2016b+ approach of using datetime() objects does modify the axes properties to know to continue to use date format when XTick are changed.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by