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

조회 수: 2 (최근 30일)
Alexei M
Alexei M 2018년 11월 27일
댓글: Alexei M 2018년 11월 28일
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에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by