MATLAB: MatLab (Image Processing/Image Acquisition) How to use trigger/getsnapshot if the preview(vid1) is on a different callback

image acquisitionImage Acquisition Toolboximage processingmatlab gui

I already asked this but I will try it in different approach.
I am getting an error saying
winvideo: The device associated with device ID 1 is already in use.
A new videoinput object cannot be created for this device while it is in use.
My preview code is:
vid1 = videoinput('winvideo', 1, 'MJPG_1024x576');
vid1.FramesPerTrigger = 1;
vid1.ReturnedColorspace = 'rgb';
triggerconfig(vid1,'manual');
vid1Res = get(vid1, 'VideoResolution');
imWidth = vid1Res(1);
imHeight = vid1Res(2);
nBands = get(vid1, 'NumberofBands');
hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axes7);
axes(handles.axes7);
preview(vid1, hImage);
and my Capture code is:
vid1 = videoinput('winvideo', 1, 'MJPG_1024x576');
vid1.FramesPerTrigger = 1;
vid1.ReturnedColorspace = 'rgb';
triggerconfig(vid1, 'manual');
vid1Res = get(vid1, 'VideoResolution');
im1Width = vid1Res(1);
im1Height = vid1Res(2);
n1Bands = get(vid1, 'NumberOfBands');
h1Image = image(zeros(im1Height, im1Width, n1Bands), 'parent', handles.axes7);
preview(vid1, h1Image);
I know that the error is insanely obvious but I really can't figure out on how to capture the preview using different button. Please guide me dear Sir(s)/Mam(s). Thank you.

Best Answer

You only need to call videoinput() once, not every time you capture a snapshot. Once you've attached your program to the camera, you are done - you don't need to do it every single time, in fact you can't so that's why you're getting the error. Make vid1 a global variable so that you can see it wherever you need to use it (whatever callback).
Related Question