Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem creating animation output file from Matlab m-file

2,140 views
Skip to first unread message

Kate J.

unread,
Apr 7, 2012, 9:31:40 PM4/7/12
to
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!

Kate J.

unread,
Apr 10, 2012, 2:06:19 PM4/10/12
to
Has anyone experienced this issue before? Is using the imwrite() function the best choice for what I want to do?

Phil Goddard

unread,
Apr 11, 2012, 10:27:17 AM4/11/12
to
Although it sometimes seems like it code never "randomly" does anything.
There must be something else going on related to setting up the code for it to run without error once but then not on another occasion.

Looking at the doc from imwrite it seems that there are quite a few restrictions for appending to gif files to make an animated gif.

The easy solution it to create an appropriate 4-D matrix within the loops and then just do one write of the whole data set at the end.
Depending on you set-up you may run into memory limitations when doing this.

Phil.

Amithraj V

unread,
Apr 13, 2012, 2:43:13 PM4/13/12
to
"Kate J." wrote in message <jm1sqr$nm4$1...@newscl01ah.mathworks.com>...
> Has anyone experienced this issue before? Is using the imwrite() function the best choice for what I want to do?

I did have a similar problem.
Within the for loop, you need to add an extra condition:
if i == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else

I think the above correction would remove the 'GIF89a format' error.
Also, in my case when I ran the code on a WIndows 7 machine, the gif file had the same first frame repeated over the duration of the loop. But when I ran the same code on a Centos system and opened it using the firefox browser, the animation was working perfectly.

Kate J.

unread,
Apr 13, 2012, 10:59:32 PM4/13/12
to
Thanks for your help, Amithraj. I added your extra condition within my loop, and indeed, my runtime error disappeared, and the animation was created. However, similar to your experience, the whole ~5 MB movie just contained the first frame of the animation.

Since I don't want to be restricted to using a particular OS or browser to display the animation, I'm going to investigate other options; I'll post again if I arrive at a general solution.

Kate J.

unread,
Apr 30, 2012, 11:54:07 PM4/30/12
to
>> The easy solution it to create an appropriate 4-D matrix within the loops and then just do one write of the whole data set at the end.
>> Depending on you set-up you may run into memory limitations when doing this.

Is there a reason why it has to be a 4-D matrix? I ran across the following tutorial:
http://homepages.udayton.edu/~rhardie1/ECE203/animated.htm

... and have tried to use the syntax suggested there:

--------------------------------------------
for i = 1:lastval

% create animation plots

M(i) = getframe;

end

movie(M);

movie2avi(M, 'armanimation.avi');
----------------------------------------

However, I get the following error message about the movie() call:

??? Error using ==> hgMovie
Could not create movie frame

Error in ==> movie at 43
builtin('hgMovie',varargin{:});

Error in ==> moviescript at 72
movie(M);


It *seems* that the movie(M) call *should* work -- any ideas about why the simple syntax above is causing this error? Note that when I execute the code in the linked example, an .avi output file *is* created, but it's only 752 kB large, yet when I attempt to play it, I get the error message that my system is running low on memory...

Thanks.

Phil Goddard

unread,
May 1, 2012, 12:44:09 AM5/1/12
to

> Is there a reason why it has to be a 4-D matrix?

From the doc for imwrite (http://www.mathworks.com/help/techdoc/ref/imwrite.html):

"When writing multiframe GIF images, X should be an 4-dimensional M-by-N-by-1-by-P array, where P is the number of frames to write."

> It *seems* that the movie(M) call *should* work -- any ideas about why the simple
> syntax above is causing this error?

Do the examples in the doc for movie, getframe, and movie2avi work?

Phil.

Kate J.

unread,
May 1, 2012, 12:29:07 PM5/1/12
to
>> Do the examples in the doc for movie, getframe, and movie2avi work?

They appear to, i.e. no error messages are generated, and an .avi file *is* created. However, as I mentioned above, the .avi file won't actually *play/run*, despite having a size of <1 MB; I get "system is low on memory" errors, and I'm not sure what the ultimate cause of that problem is.

David Verrelli

unread,
May 19, 2014, 2:35:13 AM5/19/14
to
Hello,
I think the problem may be caused by input images of inconsistent dimensions (height and width in pixels).

For example, I tried creating an animated GIF using images of mixed sizes, mixing landscape and portrait orientations.
MATLAB will generate the file with no complaints.
Playback in some viewers will show all of the images (although the result is not so pretty). Playback in other viewers will not continue past the first set of consistently-sized images. This was checked on a single computer, i.e. was not dependent upon the operating system.

A workaround is to pre-process the images (in MATLAB or outside MATLAB) to crop them, pad them, shrink them, stretch them or rotate them, so that they all end up having the same dimensions.

My reading of the documentation suggests that for writing animated GIFs a 4D matrix is not supported as an input.

You can also try creating AVI files etc., but should still pay attention to check that images have consistent dimensions.

—DIV
0 new messages