How to interrupt an executing while loop in GUI by clicking another 'EXIT' button without error for calling deleted Objects

Hi, Can anyone help me solve this problem? I don't quite know the process of interrupting a while loop by clicking another button in a unpredictable time. My design is:
I designed two push button: Control button, with a while loop, to start/stop plot something dynamically on several AXES objects; EXIT button to exit the interface and clear everything. My problem is if I want to exit without any error, I need to first click the Control button to stop the dynamical plotting, then I could click EXIT button. If I click the EXIT button while the plotting is going, I will get error message for executing the while loop.
Followed is the button callback part and the error messages? ***************************************************
function Control_Callback(hObject, eventdata, handles)
% hObject handle to Control (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.str = get(handles.Control,'String');
guidata(hObject, handles);
set(handles.Exit,'string','EXIT')
if strcmp(handles.str,'Stop')
set(handles.Control,'String','Resume plotting')
else
set(handles.Control,'String','Stop')
end
while (strcmp(get(handles.Control,'String'),'Stop'))
handles.gx =-1 + 2*rand(1);
handles.gy =-1 + 2*rand(1);
handles.gz =-1 + 2*rand(1);
handles.magnitude = (handles.gx^2+handles.gy^2+handles.gz^2)^0.5;
handles.magdata = [handles.magdata(2:end);handles.magnitude];
handles.gxdata = [handles.gxdata(2:end);handles.gx];
handles.gydata = [handles.gydata(2:end);handles.gy];
handles.gzdata = [handles.gzdata(2:end);handles.gz];
guidata(hObject,handles); % use this to save all those previous data to relize the function of "Pause"
axes(handles.axes1);
cla;
line([0 handles.gx], [0 0],[0 0], 'Color', 'r', 'LineWidth', 2, 'Marker', 'o');
line([0 0], [0 handles.gy],[0 0], 'Color', 'g', 'LineWidth', 2, 'Marker', 'o');
line([0 0], [0 0],[0 handles.gz], 'Color', 'b', 'LineWidth', 2, 'Marker', 'o');
line([0 handles.gx], [0 handles.gy],[0 handles.gz], 'Color','k', 'LineWidth', 2, 'Marker', 'o');
%limit plot to +/- 1.25 g in all directions and make axis square
handles.limits = 2.5;
axis([-handles.limits handles.limits -handles.limits handles.limits -handles.limits handles.limits]);
axis square;
grid on
xlabel('X-axis acceleration')
ylabel('Y-axis acceleration')
zlabel('Z-axis acceleration')
axes(handles.axes2);
plot(handles.index,handles.magdata,'k');
axis([1 handles.buf_len -3.5 3.5]);
xlabel('time');
ylabel('Magnitude');
axes(handles.axes3)
plot(handles.index,handles.gxdata,'r', handles.index,handles.gydata,'g', handles.index,handles.gzdata,'b');
axis([1 handles.buf_len -3.5 3.5]);
xlabel('time');
ylabel('Components');
drawnow
end
% --- Executes on button press in Exit.
function Exit_Callback(hObject, eventdata, handles)
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% to void the while loop condition
set(handles.Control,'String','Resume plotting')
% pause to wait for finishing the last while loop
pause(0.05)
clc
clear all
close all
Depending on the time I click EXIT button and the pause time, I can get errors to different part of the while loop.
*********************************************
Error using handle.handle/get Invalid or deleted object.
Error in untitled>Control_Callback (line 125) while (strcmp(get(handles.Control,'String'),'Stop'))
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in untitled (line 48) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)untitled('Control_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
********************************************
Error using axes Invalid object handle
Error in untitled>Control_Callback (line 164) axes(handles.axes3)
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in untitled (line 48) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)untitled('Control_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

 채택된 답변

Modify the Exit callback:
if strcmp(get(handles.Control, 'String'), 'Stop')
% Let the loop finish at first!
set(handles.Control, 'String', 'Wait');
drawnow;
return;
end
... do your cleanup here
And insert after the WHILE loop:
if strcmp(get(handles.Control,'String'), 'Wait')
Exit_Callback(handles.Control, [], handles);
end
Now hitting the EXIT button allows to finish the WHILE loop accurately at first and trigger the EXIT callback again afterwards.

댓글 수: 1

Thanks Jan! That's a great solution by calling Exit_Callback in the WHILE loop! I learnt from you on using return and call another button's callback function. Just one more 'return' should be added:
if strcmp(get(handles.Control,'String'), 'Wait')
Exit_Callback(handles.Control, [], handles);
return
end
I also got another similar solution to share:
****************************************
insert the following inside the while loop at the end after command 'drawnow'
if get(handles.Control,'Userdata')%x == 12
clc % close if the EXIT button has been pressed
close all
clear all
return % or use break
end
% --- Executes on button press in Exit.
function Exit_Callback(hObject, eventdata, handles)
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~strcmp(get(handles.Control,'String'),'Stop')
clc % close if the plotting has been stop in advance
close all
clear all
else
set(handles.Control,'Userdata',1)
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

You could always check: ishandle(handles.h) before trying to get()

카테고리

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

질문:

Bin
2013년 2월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by