Plotting negative x values in App Designer

9 views (last 30 days)
I am new to MatLab (and App Desiger), and am trying to plot a function that will respond to values adjusted with three different sliders. I have the sliders working now, but I am having a difficult time with my x-axis. Specifically, no negative values of x will be plotted even when I specify that x can be negative using linspace and also when specifying the XLim. All of my x values seem to be +1 of what they should be...
I've tried reading the forums and I believe it is because there is "no non-positive indexing" in MatLab.... but I am rather stuck now on how to proceed. Any appreciate is greatly appreciated!
function Plot2ButtonPushed(app, event)
x = linspace(-10,10);
V = app.V_1Slider.Value; % in eV
k = 8.6173*10^(-5); %eVK^-1
h = 4.1357*10^(-15); % in eVs
lla = app.Slider_3.Value;
T = app.TSlider.Value; %in K
%let ln(ket) = lket
a = log((2*pi/h)*V.^(2)*1/sqrt(4*pi*lla*k*T));
lket = a-(((lla + (-1)*x).^(2))/4*lla*k*T);
plot(app.UIAxes_2,lket,'b')
app.UIAxes_2.XLim = [-10 10];

Accepted Answer

Voss
Voss on 15 Jul 2022
Try this:
plot(app.UIAxes_2,x,lket,'b')
Generally arguments to plot come in x/y pairs. That is, you specify the x values and then the corresponding y values for each line you want to plot. If you only specify a single vector, then that's treated as y, and it is plotted against the indices 1:numel(y).
So you are correct: the reason you don't get any negative x values is because you've given plot one vector to plot. Give it two vectors and you'll get a plot with negative x (if there are negative values in the x you give it, of course), as above.
  2 Comments
Victoria L
Victoria L on 16 Jul 2022
Thank you very much for the clear clarification on vectors-- it is running perfect now with the two vectors included in plot!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!