Limit x-axis to specific values in GUI
Show older comments
Hello, I have a pushbutton which plots any function the user typed in an editable text. I also have two sliders, slider1 and slider2, who should dictate the limits of the x-axis of my plot. So slider1 should give the lower bound and slider2 the upper bound of the x-axis to display. My code doesn't work, I hope someone can help me with this:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
f = get(handles.edit1, 'string')
x = 0:0.1:1;
m = get(handles.slider1, 'Value')
n = get(handles.slider2, 'Value')
set(handles.axes1, 'XLim', [m, n])
axes(handles.axes1)
plot(eval(f))
Thank you!
7 Comments
Robert Eisenbecker
on 23 Oct 2017
Edited: Robert Eisenbecker
on 23 Oct 2017
Adam
on 23 Oct 2017
Then it sounds as though you are setting the limits to be outside of the valid range of the plot.
Robert Eisenbecker
on 23 Oct 2017
It's hard to give any intelligent answer if you don't show what the function is that is being passed to eval and what your sliders are being set to. 'x' is not a function so no wonder it does not plot anything. You have to plot actual values when using plot, you can't just give it an equation.
How do you know the values of your sliders after moving them? Are they (correctly) attached to edit boxes or are you just guessing the values?
Robert Eisenbecker
on 24 Oct 2017
Robert Eisenbecker
on 24 Oct 2017
Answers (1)
Jan
on 24 Oct 2017
Avoid setting the current axes actively. Better define the parent object to draw in:
% Instead of
axes(handles.axes1)
plot(eval(f))
% use
plot(handles.axes1, eval(f))
Is the 'NextPlot' property of the axes set to 'add'? This is equivalent to hold('on'). Otherwise a plot() command might reset the limits.
If the plot vanishes when the sliders are used, it sounds, like the problem is in the slider's callback, not in the one of the pushbutton.
1 Comment
Robert Eisenbecker
on 24 Oct 2017
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!