MATLAB: Unable to execute getdata in subfunction

frame capturegetdataguiImage Acquisition Toolboxwebcam

Hi, i have created a gui to capture image from webcam. however, there is error message whenever i press the push button. if codes in function cap is in main function, the work done properly. unfortunately my project requires me to capture image after pressing the push button.
can anyone help me pls?
my code
figure('MenuBar','none','Name','FYPGUI','NumberTitle','off','Position',[100,100,900,700],'resize','off');
uicontrol('Style','PushButton','String','Push','Position',[20,100,60,20],'CallBack',@cap);
display=axes('Units','Pixels','Position',[120 120 640 480]);
vid = videoinput('winvideo',2,'YUY2_640x480');
vidRes = get(vid, 'VideoResolution');
nBands = get(vid, 'NumberOfBands');
hImage = image( zeros(vidRes(2), vidRes(1)) );
vid.FramesPerTrigger = 1;
preview(vid,hImage);
start(vid);
function cap(hObject,event,vid)
global vid;
frame = getdata(vid);
figure,imshow(frame);
clear all;
end
The error message is:
??? Undefined function or method 'getdata' for input arguments of type 'double'.
Error in ==> FYPGUI>cap at 44
frame = getdata(vid);
??? Error while evaluating uicontrol Callback

Best Answer

global variables must be declared as global in every function they are used in. You do not declare vid to be global in your main script.