How to stop while loop in one buttons's callback function from another button's callback function in Gui?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a while loop in one button's callback function which creates a webcam object and takes a snapshot. The other button clears the camera object, closes the GUI and opens another GUI but because the while loop is still running in the first button's callback function I get an error saying that the camera object is deleted so cannot take snapshot. How can I remove this error? Possibly stop the first button callback's while loop when the second button is pressed. I am deleting the camera object because the second GUI needs to create a new camera object of the same webcam so I have to delete it in the first GUI.
0 Kommentare
Antworten (1)
Geoff Hayes
am 6 Nov. 2016
Momin - you can use exit the while loop by using a flag that is set in the second push button callback. For example,
function pushbutton1_Callback(hObject, eventdata, handles)
handles.cameraCleared = 0;
guidata(hObject,handles);
% do something
while true
handles = guidata(hObject); % refresh the handles data
if handles.cameraCleared
break;
end
% else the camera object still exists so keep doing your stuff
pause(0.001); % need a pause so that this function is interruptible
end
In the second pushbutton callback, we set this flag because we have cleared the camera object.
function pushbutton2_Callback(hObject, eventdata, handles)
handles.cameraCleared = 1;
%etc.
In the first callback, we need to refresh the handles structure. If we don't, then we will always be using the local (old) copy of handles that we started with.
Try the above and see what happens! (This code assumes that you are using GUIDE to develop your GUIs.)
1 Kommentar
Dang Le Minh
am 7 Feb. 2019
Thank you so much. It works. Even though it does increase the time operation for the function but it is okay.
Siehe auch
Kategorien
Mehr zu Desktop finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!