MATLAB: The device associated with device ID 1 is already in use. ERROR!!! . One webcam used for function and gui display??

errorglobalguiimage acquisition

I am trying to use a webcam in a function that i run with the gui while at the same time displaying the live video of the webcam in my gui. I can so far get the live video on my gui but when I try to start my function with the gui i get this error below. I have made vid global in my function not the gui and that's how i got the live video to display on the gui finally but i still cant figure out what to do to get both the function and my gui using the one webcam.
Thanks in advance!!!!!!
Error using videoinput (line 379)
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.
Error in AutofocusGUIGOOD (line 8) vid = videoinput('winvideo', 1, 'YUY2_640x480');
Error in LIBSGUI624>pushbutton1_Callback (line 89) AutofocusGUIGOOD(Ss , SR);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in LIBSGUI624 (line 43) gui_mainfcn(gui_State, varargin{:});

Best Answer

Hi Richard - that is quite a number of lines of code that you pasted in the comment. In the future, please either format the code so that it is readable (just highlight the code portions and press *Code {}*0) or attach the code files to your question using the paperclip button.
You must have removed the vid = videoinput('winvideo', 1, 'YUY2_640x480'); line from AutofoucsGUIGOOD and are attempting to use vid as a global variable. In order to indicate that a variable is global, the function must do so prior to using the variable. Something like
global testVar;
testVar = 42;
Every function that references this global variable (either to initialize it or to make use of it in some way) must use this global varName; statement before using the variable. That is how it is done, but I am going to recommend that you DON'T follow this approach.
In the LIBSGUI626_OpeningFcn function you initialize the video input object. You can do this, but save the object to the handles structure whose purpose is not only to manage the list of handles to widgets on your GUI but can also be used to manage user defined data...like your video input object. Just do the following
% instantiate the video input object as a field within the handles struct
handles.vid = videoinput('winvideo', 1, 'YUY2_640x480');
% do same code as before just use handles.vid in place of just vid
axes(handles.axes1);
hImage=image(zeros(160,120,3),'Parent',handles.axes1);
preview(handles.vid,hImage);
% now save the updated handles object
guidata(hObject,handles);
Now the video input object can be referenced from wherever the handles structure is available. Such as in the pushbutton1_Callback function.
Within this function do the following to access the video input object and pass it as a parameter to the AutofoucsGUIGOOD function
if ~isempty(Ss) && ~isempty(SR) && isfield(handles,'vid')
AutofocusGUIGOOD(Ss, SR, handles.vid);
end
Almost done. If the video input object has been defined in the handles structure, then we pass it as the third input to AutofocusGUIGOOD.
Now just modify the AutofocusGUIGOOD function signature to the following
function AutofoucsGUIGOOD(Ss, SR, vid)
And that is it - the video input object is instantiated in the GUI, saved to the handles structure and passed along through the pushbutton1 callback to the AutofoucsGUIGOOD function.
Just remember that since you have opened the video input object, you need to close it (or delete it) as well. Make sure that is captured somewhere in the GUI (maybe in the figure _DeleteFcn).