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

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 Comment

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??

Sign in to comment.

 Accepted Answer

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 Comments

I implemented a simple gui (button, axis) with your code and some random numbers. It works - although your code plots to a new figure window and not the one in the gui. Is that intentional?
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% ...
S2(:,1) = 1:100;
S2(:,2) = -(S2(:,1)-50).^2 ./25 + 100;
h = figure;
handles.f_low = 0;
handles.f_high = 100;
handles.f1_peak = 50;
plot(S2(:,1),S2(:,2));
xlim([handles.f_low,handles.f_high])
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);
line([handles.f1_peak,handles.f1_peak],get(gca,'YLim'),'Color','k');
What does your data/code look like?
Yes, it is intentional that it plots to a new figure and not the one in the GUI.
Is your Matlab version the same as mine (2018a)?
Should I post my full code and sample data?
xline was introduced in 2018b.
If you want to attach it, that is the easiest to see what you are doing, but you can also post just the relevant sections here. At least the entire function that is creating the plot would be helpful.
Ok, first, maybe this will be helpful without overwhelming with too much detail. I'm attaching a screen shot of my code executing in debug mode, and you can see the resulting output in the figure window. This shows the details of what I outlined in my comment on my OP, rather than my actual OP.
I have essentially two identical code lines, where it's supposed to plot two vertical lines at two different locations: code lines 587 & 589. I changed the first of those lines so that it takes the new variable y_axis_limits instead of using the get command. The second line is how I had it originally, before changes. You can see that 587 failed to execute properly, because there should be a vertical black line on the left of the peak. Line 589 executed correctly, and you can see the vertical line to the right of the peak. (In my code f1_peak < f2_peak, so that's why line 587 should produce a line to the left of whatever line 589 makes.)
Alexei, can you upgrade? If you have an active software maintenance agreement, you can. That could be the easiest fix and way to follow Cris.
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')
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!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2018a

Tags

Asked:

on 27 Nov 2018

Commented:

on 28 Nov 2018

Community Treasure Hunt

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

Start Hunting!