How to stop while loop in one buttons's callback function from another button's callback function in Gui?

4 vues (au cours des 30 derniers jours)
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.

Réponses (1)

Geoff Hayes
Geoff Hayes le 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.)

Catégories

En savoir plus sur Desktop dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by