Reputation: 11
newish Matlab coder here seeking advice on my first GUI. I'm building a simple GUI that plots 2 lines on the same graph (axes1) and I'm trying to utilize sliders to change the slope of each line independently to the value of the slider it is associated with. I have created the plot, and am able to change the slope of each line using slider callbacks; however, when I change the slope of line-2 after having changed the slope of line-1, the slope value of line-1 reverts back to its initial so the graph plots the changing line-2 slope with the line-1 slope remaining at its initial value. The same occurs to line-2 when trying to change the slope of line-1.
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)
handles.gNa = get(hObject,'Value')
plot_axes1(hObject, eventdata, handles);
end
function slider3_Callback(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.gK = get(hObject,'Value')
plot_axes1(hObject, eventdata, handles);
end
function plot_axes1(hObject, eventdata, handles);
Vk = -77;
Vna = 50;
V = (-80:0.1:60);
Ik = handles.gK*(V - Vk);
Ina = handles.gNa*(V - Vna);
axes(handles.axes1);
plot(V, Ik, V, Ina);
end
Also, When handles.gK or handles.gNa prints (when either the slider callbacks occur), it prints the initial values of the handle variable opposite to the one being changed via the slider.
I have followed the guidance of others and instituted a separate plotting function, which is called by each sliders' callback function but can't seem to get the slope values to remain at the slider value. Any help on how to retain the slope of each line while the other is being altered would be greatly appreciated. Thanks!
Upvotes: 1
Views: 448
Reputation: 74940
When you use the handles
structure to store additional data, you need calls to guidata
to ensure that the handles structure remains updated.
All you need to do at the end of your callbacks (at the earliest after you have assigned the values) is to add a line
guidata(hObject,handles);
Upvotes: 1