How can I make this line start from a specific date?

Hello everyone,
How can I make the magenta line start from a specific date (14 Jan 2015) in the graph, instead of 1 Jan 1998?
% Lv = DTv <= datetime('7-Jan-2020');
Thank you!

 Akzeptierte Antwort

Star Strider
Star Strider am 4 Sep. 2021
Change ‘Lv’ to:
Lv = (DTv <= datetime('7-Jan-2020')) & (DTv >= datetime('14-Jan-2015'));
and it should do what you want.
Consider adding:
xlim([min(DTv(Lv)) max(DTv(Lv))])
after the plot calls that use ‘DTv’ to restrict the x-axis limits. This is not required, however it would avoid whitespace at the ends of the plots.
.

6 Kommentare

Thank you!
It gives me error when I add " xlim([min(DTv(Lv)) max(DTv(Lv))])"
">> xlim([min(DTv(Lv)) max(DTv(Lv))])
Error using xlim (line 31)
Limits must be a 2-element vector of increasing numeric values."
As always, my pleasure!
The code I posted ran without error on my computer offline (R2021a Update 5).
Specifically, extracting that vector:
xlim_vector = [min(DTv(Lv)) max(DTv(Lv))]
produced:
xlim_vector =
1×2 datetime array
14-Jan-2015 07-Jan-2020
I inserted that xlim call only after the second figure, so that may be the issue, although I deifined ‘Lv’ just after defining ‘DTv’ so that part of the code is now:
DTv = datetime(DATIECMWFgiornalieri{:,1:3});
Lv = (DTv <= datetime('7-Jan-2020')) & (DTv >= datetime('14-Jan-2015'));
smb=table2array(DATIECMWFgiornalieri(:,4));
smb = fillmissing(smb, 'constant',0);
Cum=cumsum(smb);
Cum_smbp=DATIECMWFgiornalieri;
Cum_smbp(:,4)=array2table(Cum);
figure
% plot(x,y,'g', 'DisplayName')
hold on
plot(DTv(Lv),table2array(Cum_smbp(Lv,4),Cum_smbp.SMB_larsenmm(1:8042)),'m');
xlim([min(DTv(Lv)) max(DTv(Lv))])
That ran without error.
The
>>
prompt leads me to believe that you are running it from the Command Window instead of a script. That could be the problem, although I have no way of determining that exactly, or the reason that would have failed.
.
Pul
Pul am 5 Sep. 2021
Bearbeitet: Pul am 5 Sep. 2021
No, I just copied the error I got from the Command Window, but I runned the code in a proper way!
Now I got this. The first plot is right but on the x-axis "Jan" appears instead of just years while the second plot is just the repetition of the first one, but with a white space at the end of the graph.
Replace:
xlim_vector = [min(DTv(Lv)) max(DTv(Lv))]
with:
xlim([min(DTv(Lv)) max(DTv(Lv))])
For the third plot, see if:
xtickformat('yyyy')
does what you want.
.
Pul
Pul am 5 Sep. 2021
Yes, perfect.
Thank you!
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Gefragt:

Pul
am 4 Sep. 2021

Bearbeitet:

Pul
am 5 Sep. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by