Matlab GUI: Plot bounds change but graph stays the same.
Afficher commentaires plus anciens
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.
1 commentaire
Stephen23
le 30 Mar 2015
Note that it is recommended to avoid eval as much as possible. This advice comes up clearly in many discussions here too:
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!