title: Matlab: Writing avi file
body: <p>I am having a problem when creating an avi file using Matlab. My aim is to use an edge filter on an entire video and save the file as an avi. The filter works fine, my problem is the writing of the avi file.</p>
  
  <p>My code:</p>
  
  <pre><code>vidFile = VideoReader('video.avi');
  edgeMov = avifile('edges','fps',30);
  
  for i = 1:vidFile.numberofframes
    frameI = read(vidFile,i);
    frameIgray = rgb2gray(frameI);
    edgeI = edge(frameIgray,'canny',0.6);
    edgeIuint8 = im2uint8(edgeI);
    edgeIuint8(:,:,2) = edgeIuint8(:,:,1);
    edgeIuint8(:,:,3) = edgeIuint8(:,:,1);
    edgeMov = addframe(edgeMov,edgeIuint8);
  end
  
  edgeMov = close(edgeMov)
  </code></pre>
  
  <p>When the loop finishes and the avifile is closed, I go to play the video and it says "Windows Media Player encountered a problem while playing this file". I've also tried, without success, Media Player Classic and VLC which lead me to believe that the problem must be the file itself. Using GSpot I checked the file and it said that the AVI header is corrupt.</p>
  
  <p>Retrying the loop again returned exactly the same problem. What's confusing me is when I run the loop for a smaller number of frames, 30 for example, the video writes fine and I can watch it. The size of the video I am trying to convert is in excess of 1000 frames so I don't know if size is a problem?</p>
  
  <p>Any help would be greatly appreciated, thank you.</p>