MATLAB Answers

hold on y-axis

6 views (last 30 days)
GIOVANNA GUADAGNIN
GIOVANNA GUADAGNIN on 11 May 2021
i want to plot axes parallel to y-axis. I got the limits for y easily and
on the x-axis I have dates like '2020-11-17 13:03:00'.
this is my plot
figure
yyaxis left
plot(T.UTC_Date___Time,mareaCM,'b--');
ylim
yyaxis right
plot(T.UTC_Date___Time,T.DissolvedOxygenSaturation,'r--');
hold on
%I tried with this but it doesn't work
plot([2020-11-17 13:03:00 2020-11-17 13:03:00],[0 450],'g--')
  1 Comment
Andy
Andy on 13 May 2021
Do any error messages appear or is it that Matlab doesn't report a problem but the line doesn't appear where you think it should?

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 13 May 2021
%I tried with this but it doesn't work
% plot([2020-11-17 13:03:00 2020-11-17 13:03:00],[0 450],'g--')
No, that's not going to do what you expected. If you want to plot using date and time data on the X axis, turn that data into a datetime array first.
s = ['2020-11-17 13:03:00'; '2020-11-17 13:03:00']
s = 2×19 char array
'2020-11-17 13:03:00' '2020-11-17 13:03:00'
dt = datetime(s)
dt = 2×1 datetime array
17-Nov-2020 13:03:00 17-Nov-2020 13:03:00
In this case using the xline function is probably what you want instead of plotting using the same X values twice. That will keep the line spanning the whole height of the axes even if you pan or change the Y axis limits.
xline(dt(1), 'g--')
  1 Comment
GIOVANNA GUADAGNIN
GIOVANNA GUADAGNIN on 16 May 2021
thank you!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!