Vertical line refuses to plot after tweaking YLim slightly. Why??

11 次查看(过去 30 天)
I have working code that draws vertical lines on a plot. Here's the line of code to first create the initial plot:
h = figure; plot(S2(:,1),S2(:,2)); xlim([handles.f_low,handles.f_high])
I'm using a GUI interface created in GUIDE, which is why there are references the handles structure. The x-axis limits are defined by me, but Matlab automatically determines the y-axis limits from the plot command. There a few other non-essential things like adding plot title and making the y-axis into log scale. The essential (and troublesome) line of code is this:
line([handles.f1_peak,handles.f1_peak],get(gca,'YLim'),'Color','k');
It worked fine initially. Then I tried adding lines of code preceding this which tweak the automatically generated y-axis limits by making them slightly larger.
y_axis_limits = get(gca,'YLim');
y_axis_limits(2) = y_axis_limits(2) + y_axis_limits(2)*.1;
set(gca,'YLim',y_axis_limits);
When I run the debugger it seems that everything is fine, it just makes the y-axis scale a little higher at the top without affecting bottom cutoff. But this results in the line command completely breaking without any error message or anything. It just doesn't do anything! That line of code executes, and nothing happens. As if it drew the line somewhere outside of the plot limits?? I don't get it..........
  1 个评论
Alexei M
Alexei M 2018-11-27
Alright I experimented further. I removed the lines in which the YLim is adjusted. I tried comparing the following:
1.)
line([handles.f1_peak,handles.f1_peak],get(gca,'YLim'),'Color','k');
2.)
y_axis_limits = get(gca,'YLim');
line([handles.f1_peak,handles.f1_peak],y_axis_limits,'Color','k');
It turns out that 1.) works (as before). But 2.) does not work. Wtf??

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2018-11-27
If you are using the latest version of MATLAB, check out the xline function that was just introduced. Just tell it at what value of x you want the vertical line, and it does the rest.
  7 个评论
Cris LaPierre
Cris LaPierre 2018-11-27
编辑:Cris LaPierre 2018-11-28
I think I got it. You capture y_axis_limits in line 566. For my example, it is [0 100]. But then in line 578 you change the Yscale to log. This changes YLim (in my example) to [3.96 100].
The problem is the value 0 does not exist in a log scale. You can get close, but never there. Since y_axis_limits(1) = 0, the first value can't be plotted, so it fails to add the first line. Try commenting out line 578 and see if you can get both lines. I bet you will.
The second plot is still using get(gca,'YLim'), and gets valid values and can be added.
My recommendation is to put the set(gca,'Yscale','log') before y_axis_limits = get(gca,'YLim')
Alexei M
Alexei M 2018-11-28
Oh you're right!
Yeah... I played around with it, and now it all behaves as I expected, simply by doing the log operation before everything else. Dang, silly of me to overlook this issue....
Thank you very much for the solution!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by