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
handles.vid = videoinput('winvideo', 1, 'YUY2_640x480');
axes(handles.axes1);
hImage=image(zeros(160,120,3),'Parent',handles.axes1);
preview(handles.vid,hImage);
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).
Best Answer