I'm trying to animate an image and save it as a .gif file. After crafting an equation, I use imshow within a loop and capture successive frames with getframe, which I then convert to an image and again into an indexed file. I've tried other approaches to this but always came up with the same problem. Even the sample 'peaks/surf' function in Matlab's documentation on creating animated gifs doesn't output the correct .gif file (meaning it lags because of repeated frames).
The problem I'm having is that it appears that at seemingly random times, getframe captures the previous frame rather than the current one. The issue does not occur on other computers I've tried it on, so I have a feeling it's a problem with the communication between my cpu and graphics processor (Mac OSX 10.7.5, Macbook Pro w Retina display 17"). It's almost as if the CPU is processing images faster than the GPU can send them to my monitor. Any insights on this theory are appreciated. I've tried updating Java and Matlab (v.2012a) to no avail. I also installed the recent updates for OSX, one of which addressed frame refresh rates for video games.
When I give the computer some time between the imshow and getframe operations by uncommenting the 'close' command (see code below) so that getframe cannot possible capture the wrong image, the .gif comes out perfectly. Otherwise, I get repeated frames in the output (see images in imgur album). The issue I have with using 'close' is that it really lengthens the execution time (about 4x).
My code is below, which is likely to produce the desired result without uncommenting 'close' on most people's computers. I realize it's a tough issue to tackle because it seems to be related to my GPU but I'm at a loss for what to do here... Thank you for your time.
if i<(endloop/2 + 2)
fl = log(1+abs(cf)); fm = max(fl(:));
imwrite(imind,map,'circle3.gif', 'DelayTime',0.1, 'WriteMode', 'append');