필터 지우기
필터 지우기

Trouble with datetick x-axis...always starts at 1/1/00

조회 수: 1 (최근 30일)
Cary
Cary 2015년 6월 6일
댓글: Cary 2015년 6월 6일
Here is my code:
cumret=cumprod(1+combinedRet)-1;
plot(cumret)
startDate=tday(1);
endDate=tday(end);
x=linspace(startDate,endDate,10);
datetick('x',2);
startDate is 20120824, endDate is 20150529. So why does my x-axis start at 1/1/00?

채택된 답변

Walter Roberson
Walter Roberson 2015년 6월 6일
In order to use datetick on numeric values, the values need to be in MATLAB's numeric date format. That format represents time as number of days since Jan 1, of a mythical year 0. You need to convert 20120824 to that format in order to use datetick on numeric values, The easiest way would be
startDate = datenum(num2str(tday(1)),'YYYYMMDD');
endDate = datenum(num2str(tday(end)), 'YYYYMMDD');
The next problem you have is that you datetick() works on existing x values. When you used
plot(cumret)
implicit x values of 1:length(cumret) were used. You may have calculate a variable named "x" but you have not given any connection between that variable and the axis for your plot.
What you should do is calculate startDate and endDate first (with the conversion to date numbers), and then you should
x = linspace(startDate, endDate, length(cumret));
as that defines one date value for each entry in cumret. Then
plot(x, cumret);
datetick('x', 2);
  댓글 수: 1
Cary
Cary 2015년 6월 6일
Updated code:
startDate=datenum(num2str(tday(1)),'yyyymmdd');
endDate=datenum(num2str(tday(end)),'yyyymmdd');
cumret=cumprod(1+combinedRet)-1;
x=linspace(startDate,endDate,length(cumret));
plot(x,cumret);
datetick('x',2);
Now I'm getting the right dates but the graph starts at 1/1/12 instead of 8/24/12. There is also extra room on the right even though there is no data beyond 5/29/15.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by