MATLAB: Image1.jpg, image2.jpg…..etc how

Image Acquisition Toolboximage processingImage Processing Toolbox

I want to save images without overwriting them whenever I hit the pushbutton. Can you please help me how save images without overwriting the original? What I want to do is whenever I'll hit the pushbutton, It will generated 1 image at a time without deleting the original.
here's my simple code.
vid = videoinput('winvideo',1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
imwrite(img,'C:\Users\Sony Vaio\Documents\Appendix\images\image.jpg');
thank you 🙂

Best Answer

counter = 1; %initialize filename increment
vid = videoinput('winvideo',1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
savename = strcat('C:\Users\Sony Vaio\Documents\Appendix\images\image_' , num2str(counter) , '.jpg'); %this is where and what your image will be saved
imwrite(img, savename);
counter = counter +1; %counter should increment each time you push the button
Instead of using a counter, you could also use a timestamp, as from CLOCK or DATESTR.