Trouble with datetick x-axis...always starts at 1/1/00
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Cary
am 6 Jun. 2015
Kommentiert: Cary
am 6 Jun. 2015
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?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 6 Jun. 2015
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);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!