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

1 次查看(过去 30 天)
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