フィルターのクリア

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 Center および File ExchangeCalendar についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by