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

8 次查看(过去 30 天)
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.

采纳的回答

Geoff Hayes
Geoff Hayes 2015-3-29
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 个评论
Ian Hanken
Ian Hanken 2015-3-30
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by