Matlab GUI: Plot bounds change but graph stays the same.

3 views (last 30 days)
Hello,
I'm attempting to plot a function along with its integral on two separate graphs, and I want to use a slider to change the bounds. However, when I change the bounds, it seems the functions stay the same. Perhaps a better way to put it is that the x value seems to grow, but the y-value stays the same. Here is the code:
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
global t a fct1 fct2
t = 0:0.1:10;
ca = get(handles.edit1,'string');
fct1 = sym(ca);
fct2 = int(sym(fct1));
axes(handles.axes1);
plot(t, eval(fct1));
set(handles.edit2,'String',char(fct2));
axes(handles.axes2);
plot(t, eval(fct2));
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global t a fct1 fct2
a = get(handles.slider1, 'Value');
axes(handles.axes1);
plot(t * a, eval(fct1));
axes(handles.axes2);
plot(t * a, eval(fct2));
So it takes a function in edit1, prints the integral in edit2, plots the function in axes1, plots the integral in aces2. This all works fine, but when I use the slider to try to shift the bounds, it will shift them just fine, but y value won't seem to change, and thus the graph doesn't look right. What am I doing wrong here?
Thanks.

Accepted Answer

Geoff Hayes
Geoff Hayes on 29 Mar 2015
Ian - look closely at the code in the slider callback
a = get(handles.slider1, 'Value');
axes(handles.axes1);
plot(t * a, eval(fct1));
You obtain the current value of the slider, a, and then set axes1 to be the current axes that it will be updated with the subsequent plot. Now note the plot is just
plot(t * a, eval(fct1));
which evaluates fct1 (not clear what this symbolic expression or function is) and plots the results relative to t*a but nowhere does fct1 receive the modified x-axis inputs, so it is probably (?) using the t = 0:0.1:10; that had been defined earlier. Is t a variable in your symbolic expression?
You may have to do something like
t = t * a;
and then the
eval(fct1)
will change. This is only a guess though.
As well, instead of using global variables, just save your user-defined data to the handles structure. See guidata for details and examples.
  1 Comment
Ian Hanken
Ian Hanken on 30 Mar 2015
Changed my slider code to this:
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global t a fct1 fct2
t = 0:0.1:10;
a = get(handles.slider1, 'Value');
t = t * a;
axes(handles.axes1);
plot(t, eval(fct1));
axes(handles.axes2);
plot(t, eval(fct2));
This works perfectly. You helped me out a lot here. I'll also work on using the handles structure. Thank you so much for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots 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!