MATLAB: Plot hold on in GUI

guihold onMATLAB and Simulink Student Suiteplot

I am new to matlab and need a bit of help with a gui. I have two axes boxes (axes2 and axes3) where I use checkmarks to plot within. First checkbox creates two plots: one (x1,y1) in axes2 and the second (x1,ht) in axes3. The second checkbox also creates two plots: one(x2,y2) in axes2 and the second (x2,ht2) in axes3. I want to Hold on to the figures, so if I select both checkmarks both axes2 and axes3 will contain two plots. However, only axes2 work. In axes3 only show the plot connected to the last selected chechmark.
Please help. The code is below:
function checkbox1_Callback(hObject, eventdata, handles)
if get(handles.checkbox1,'Value')
hold on;
handles.hCbox1Plot = plot(x1,y1,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes2);
hold on;
handles.hCbox2Plot = plot(x1,ht,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes3);
guidata(hObject,handles); % do this to save the updated handles structure

else
if ~isempty(handles.hCbox1Plot);
~isempty(handles.hCbox2Plot);
delete(handles.hCbox1Plot);
delete(handles.hCbox2Plot);
end
end
function checkbox2_Callback(hObject, eventdata, handles)
if get(handles.checkbox2,'Value')
hold on;
handles.hCbox3Plot = plot(x2,y2,'LineWidth',2,'Color', [1 0 0],'parent',handles.axes2);
hold on;
handles.hCbox4Plot = plot(x2,ht2,'LineWidth',2,'Color', [1 0 0],'parent',handles.axes3);
guidata(hObject,handles); % do this to save the updated handles structure
else
if ~isempty(handles.hCbox3Plot);
~isempty(handles.hCbox4Plot);
delete(handles.hCbox3Plot);
delete(handles.hCbox4Plot);
end
end

Best Answer

doc hold
shows that you can call hold with an axes handle. You should always do this e.g.
hold( handles.axes2, 'on' )
It is far better than just calling
hold on
and hoping for the best that it will apply to the axes you expect.