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
채택된 답변
추가 답변 (1개)
Sean de Wolski
2013년 2월 5일
0 개 추천
You could always check: ishandle(handles.h) before trying to get()
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!