Creating image files from a video file in matlab

The question came up from two colleagues separately how to create  single image files from a video using matlab – either every frame or every 2nd, 5th and so on. Background is we have some cameras in the lab that produce only videos and the PIV (Particle Image Velocimetry) Software only accepts picture files.  Unfortunately there are different ways to try that in Matlab (command in opening the video file etc). This turned out to be the only reliable code snippet:

vidfile = "yourvideo.avi";
BaseName='frame_';
vid = mmreader(vidfile);

for k = 1:10:vid.NumberOfFrames
    image = read(vid,k);
    FileName=[BaseName,num2str(k),'.jpg'];
    imwrite (image,FileName);
end

If You take a look at the for loop, You will see we save every 10th picture in this example. And the pictures are saved as frame_10.jpg, frame_20.jpg …