closing matlab webcam automatically in matlab

8 views (last 30 days)
I want to automatically close the webcam each time the matlab program opens. The reason is that the main gui has many functions, and if there is an error, or the user breaks out of the code, there is no way to go back and close the webcam, it is left in an open state.
with other hardware in the past, there are ways to poll the status of the device to see if it open and then, allow it to reconnect so it can be shut down cleanly.. this being done at the start of the program.. how do it do it with the webcam..
these are some of the base commands for the webcam app im using http://www.mathworks.com/help/supportpkg/usbwebcams/ug/acquire-images-from-webcams.html#bt6eea0
ideally we have the full blow instrument and data acq toolboxes, not sure why we are using this but would be nice to finish with this.
this is some base code, not the real GUI which has many functions
framerate_desired=10; % frames per second
cam1 = 'Logitech HD Webcam C615';
% cam2 = 'Integrated Webcam';
% cam3 = 'Endoscope';
bore = webcam(cam1);
bore.Resolution = '640x360';
vidWriter = VideoWriter('frames3.avi');
vidWriter.FrameRate = framerate_desired; % output framerate
open(vidWriter);
'for index = 1:30
img = snapshot(bore);
imwrite(img, 'test.jpg', 'jpg')
writeVideo(vidWriter, img);
end
close(vidWriter);
clear bore

Answers (1)

Walter Roberson
Walter Roberson on 2 Dec 2015
"if there is an error, or the user breaks out of the code, there is no way to go back and close the webcam, it is left in an open state."
You should use an onCleanup() to close the camera. An onCleanup() is essentially attached to an object (or variable) and is executed when the item is deleted, such as if the item is a local variable in a function that exits.
onCleanup() will also be executed if the item it is attached to is clear'd.

Categories

Find more on Instrument Control Toolbox Supported Hardware in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!