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

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

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개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2015년 3월 28일

댓글:

2015년 3월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by