Video = videoinput('winvideo',2);
set(Video,'FramesPerTrigger',Inf);
set(Video,'ReturnedColorspace','grayscale');
start(Video)
imhand = imshow(zeros(2,2)); %establish an image object
while(Video.FramesAcquired <= Inf)
data = getsnapshot(Video)
if ~isgraphics(imhand); break; end %image gone, user must have deleted it
set(imhand, 'CData', data);
end
stop(Video);
If you are using R2014a or earlier, replace isgraphics() with ishandle()
