How to interrupt an executing while loop in GUI by clicking another 'EXIT' button without error for calling deleted Objects
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Jan
on 5 Feb 2013
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.
More Answers (1)
Sean de Wolski
on 5 Feb 2013
You could always check: ishandle(handles.h) before trying to get()
0 Comments
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!