I have a Matlab m-file that reads in my data and successfully displays to the screen an animation of the data. Now, I’m attempting to generate an animation *output file*, so that I can replay this animation in a portable (e.g. .gif ) format on other non-Matlab PCs.
At present, the code that I show below *does* generate an “animation” output file (of several MB in size, where one component frame is several kB). However, the problem seems to be that the output “animation” file just contains hundreds of the same frame (which is the final frame of the entire animation), rather than the correct, individual animation frames.
I’ve attempted to follow the example code shown in this Matlab Solutions link:
How can I create animated GIF images in Matlab?
http://www.mathworks.com/support/solutions/en/data/1-48KECO/?solution=1-48KECO
and here is a simplification of my code (I omitted much of my correctly-working animation code, and included the full new animation-file-generating code):
-------------------------------------------------------
filename = 'myAnimation.gif';
for i=1:Endval
% calculate and plot animation to screen
% ...
set(plot(x,y,'b',x,y,'bo', xtarget, ytarget, 'rx'),'LineWidth',3);
drawnow;
% new animation-file-generating code
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
--------------------------------------------------------
Can anyone spot what I’m doing wrong, such that the animated .gif that’s being generated only contains the final frame of my animation, rather than every intermediate frame?
Update: I tried to re-run the above code, to double-check that the behavior I described above is correct, and now, when I try to run the same code (which hasn’t been modified in any way), it won’t even run; it immediately terminates with the error message:
------------------------
??? Error using ==> wgifc
File must be GIF89a format for appending
Error in ==> writegif at 381
wgifc(mat, map, filename,writemode,userinput,disposalmethod,delaytime,...
Error in ==> imwrite at 473
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in ==> stickmovie at 66
imwrite(imind,cm,filename,'gif','WriteMode','append');
------------------------
I had seen these error messages previously, as well, before the code randomly started working.
So, in addition to the single-frame-being-made-into-the-whole-animation problem described above, does anyone have ideas about what might be causing the error messages above (when I’ve followed the example in the link above), which seem to randomly come and go when I run the code? (I’ve checked, and their occurrence doesn’t depend on any other files that are present or absent in my working directory.)
Note that I don’t call any wgifc() or feval() functions within my code; these error messages seem to be low-level problems with my using the imwrite() function (and my current syntax of my imwrite() call *does* comply with the official syntax described in the Matlab documentation).
Thanks for your help!