MATLAB: Save snapshot image in a variable within a loop!!

Image Acquisition Toolboxvideo

please need help in this why the data(i) variable in while loop give me an error here I want to take a picture after each 5 second and I need it continuous so I used while(1) …
vid = videoinput('winvideo',1);
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb');
i = 0;
while(1)
data(i) = getsnapshot(vid);
imshow(data(i));
i=i+1;
pause(5);
end

Best Answer

Get rid of the (i) and have just data, or better yet, a more descriptive filename:
for f = 1 : 1000 % view and save 1000 frames then quit.
thisFrame = getsnapshot(vid);
imshow(thisFrame);
drawnow;
baseFileName = sprintf('Frame %3.3d.png', f);
fullFileName = fullfile(folder, baseFileName);
imwrite(thisFrame, fullFileName);
end