Trouble with datetick x-axis...always starts at 1/1/00
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Walter Roberson
il 6 Giu 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);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!