how to execute stop(vid) when i close the imshow figure while running a while loop?

i am capturing images from a camera in real time and displaying the processed image in a while loop. As soon as I close the figure window, an error pops up. I want to execute stop(vid) after i close the figure and don't want the error to exist.
the error is as given below
Error using matlab.ui.Figure/set Invalid or deleted object.
Error in imshow (line 343) set(fig_handle, 'NextPlot', 'replacechildren')
Error in LiveDetector (line 40) imshow(imgOut);toc;

5 Comments

Looking at your post, the error is likely arising because the figure window is being closed before all the triggered instances of the callback function are processed, leading to the figure handle’s state becoming null for them. To avoid this, you can close the figure window after stopping the Image Acquisition and the associated cleanup.
If this is not the case, please post your code snippet here for further analysis.
is there a way to check figure handle while the loop is running. In that way i will be able to execute the stop(vid) command when the figure closes through an if loop.
Did you try adding a GUI button instead? It would look something like this:
figureHandle = figure(1);
StopButton = uicontrol('Style','pushbutton','String','Stop & Close','pos',[0, 0, 200, 25],'Callback','delete(gcbo)');
while ishandle(StopButton)
% Do something
end
So have you solved this problem,bro ? How did you achieve that ?
use the close request callback that I linked to . That is exactly the kind of purpose the callback was designed for.

Sign in to comment.

Asked:

on 14 Sep 2018

Commented:

on 12 Jan 2019

Community Treasure Hunt

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

Start Hunting!