f



Problem creating animation output file from Matlab m-file

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! 
0
kmj.work (178)
4/8/2012 1:31:40 AM
comp.soft-sys.matlab 211266 articles. 23 followers. lunamoonmoon (257) is leader. Post Follow

8 Replies
4317 Views

Similar Articles

[PageSpeed] 55

Has anyone experienced this issue before? Is using the imwrite() function the best choice for what I want to do? 
0
kmj.work (178)
4/10/2012 6:06:19 PM
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.
0
phil986 (332)
4/11/2012 2:27:17 PM
"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.
0
4/13/2012 6:43:13 PM
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. 
0
kmj.work (178)
4/14/2012 2:59:32 AM
>> 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.
0
kmj.work (178)
5/1/2012 3:54:07 AM
> 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.
0
phil986 (332)
5/1/2012 4:44:09 AM
>> 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. 
0
kmj.work (178)
5/1/2012 4:29:07 PM
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
David
5/19/2014 6:35:13 AM
Reply:

Similar Artilces:

How to set path for .M files. How to include, and verify a file in the MATLAB path. In other words I wish to learn how to setup the path for .M file in MATLAB path? Thanks.
undefined ...

How to set path for .M files. How to include, and verify a file in the MATLAB path. In other words I wish to learn how to setup the path for .M file in MATLAB path? Thanks. #2
undefined ...

Matlab freezes for a short while when saving an m-file, then overwrites m-file with blank file
I'm having a problem with saving files in Matlab. Every so often Matlab freezes for a short while when saving an m-file, then overwrites m-file with blank file and shows this in the Editor window. All the code is gone and I am left with an empty file, saved over the top of my file with the same name. I am saving to a network drive and it happens to both me and my colleague. It also happen in multiple versions of Matlab. Any ideas? So far the best I have been able to do is make sure autosave is turned on and copy the contents of the autosave into the blank file, meaning I only lose a ...

MATLAB files (M-files)
Hello All, Could anyone explain with me why MATLAB files (M-files) execute slower than compiled written in other languages such as C,C++ and Fortran? Give me the significant slow of M-files to be compiled Thank you. ...

creating exe file from m file using MATLAB R2009a version
how to create exe file from m file in R2009a version of matlab? when trying to create we get the following error >> mcc -m vk1234 Select a compiler: [1] Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2009a\sys\lcc [0] None Compiler: 1 Trying to update options file: C:\Documents and Settings\student\Application Data\MathWorks\MATLAB\R2009a\compopts.bat From template: C:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\lcccompp.bat Done . . . 'cmd' is not recognized as an internal or external command, operable program or batch file. Error: An error occurre...

Cant open MATLAB .fig files created using m-file script
I have a large number of fig plots of analysis results in various databases. These plots are generated automatically when I run my tests using saved m files. These m files are similar to those automatically generated by plottools, but modified to better suit my needs. Lately I've found that I'm unable to open these figure files. From the MATLAB, figure or plottools window, if I go to open any of these figures nothing happens. They do not open and no warning message appears. I'm at a loss to understand why this is because as far as I can recall it worked fine not so long ago. Oth...

how to call matlab file to another matlab file
Hey friends, i am working on simulation of power system using matlab. for that i have written codes for generation side and line side separately. Now i want to use the result of generation file to line file....so how can i use the result of one file to another.... eg i have gen.m file from that i can get hourly gen data....now i want to compare that data to line.....and line modelling is done in separate file line.m so i want to call result of gen.m file to line.m file..... waiting for ur valuable suggestion thanks ...

MATLAB COMPILER: compiling multiple m files that calls other m files
HI! I'm trying to compile a program writen in one m file which serves as a menu and it calls other m files (GUI's). The problem is that i have several functions (m files) in several directories (wich i�ve included in the first menu), so i want to compile all the m files wich are called by the first and so on. I�ve tryed to compile the menu doing: mcc -m menu.m, but it gives an error when i push a button to open another GUI. Can someone tell me how to compile a GUI wich calls other GUI's? Thanks Rui Deus : > > You need to tell the mcc compiler where these other m-files are...

I have a m-file that I compiled into a dll using MatLab compiler. I then created a C# library class to call the MatLab dll
hi, >i creat an M-file which has an imagen in file then in matlab i create it dll using >deploytool >Then i include this dll in visual studio 2005 C#.Net project >Then i make a windows form which has a button,picturebox,textbox openfiledialog >what should be the code of button it access the m-file from matlab show its path in >textbox and image in picture box or >what is the procedure of accesing matlab file from C#.Net,plz help me for this >problem immediatley >my code of m-file is function aa %Ori_Face=imread('E:\Shakir\Faisal\Face\sub6.1.jpg'); ...

load5
As a solution to some posts a while ago about reading compressed matlab 7 files in matlab 6, i posted load5 to the file exchange (7734) which does this. Note that the "Other Requirements" field on the FX is outdated as the zip file has both windows and linux versions and DOES search the matlab path if the file is not found in the current directory. ...

Get variable from another m-file and place it in current m-file in Matlab
Hi there, How I have a matlab file that calculates a variable "l".Now i want to call this "l" variable in another matlab file I am working on. How do i do this please? Urgent!! On 11/14/2012 4:39 AM, Francis Etu-Menson wrote: > Hi there, > How I have a matlab file that calculates a variable "l".Now i want to call this "l" variable >in another matlab file I am working on. How do i do this please? Urgent!! > The first function A() will return this variable "L" as its return value. Then you can pass this variable "L&q...

how to open .pdf files from Matlab .m files
i hav developed a software in matlab and want to add user manuals(.pdf format) to this software. i want to open these pdf files by clicking on a button on the main GUI.please if anybody can help... waiting for an urgent reply.. Thanks... muazma wrote: > > > i hav developed a software in matlab and want to add user > manuals(.pdf > format) to this software. i want to open these pdf files by > clicking on > a button on the main GUI.please if anybody can help... > waiting for an urgent reply.. > Thanks... > > Simply add ! before the pdf file you want to execute ...

convert a matlab file(*.m) to an *.exe file
How to convert a matlab file(*.m) to an *.exe file.I want to link the matlab file to a dynamic web page created using ASP. You need the matlab compiler toolbox. ...

how to connect m-file with fis file in matlab..???
hi, Do anyone know how to connect matlab m-file which written for GUI question dialog box that get input from user and check on FIS file in fuzzy logic to get the out put???? or how to call fis on m-file?? thanks for sharing your knowledge.. ...

Web resources about - Problem creating animation output file from Matlab m-file - comp.soft-sys.matlab

Animation - Wikipedia, the free encyclopedia
... in motion pictures in general—is thought to rely on the phi phenomenon . Animators are artists who specialize in the creation of animation. ...

Animation - Wikipedia, the free encyclopedia
Animations can be recorded on either analogue media, such as a flip book , motion picture film , video tape, or on digital media , including ...

Animation - Wikipedia
Animation is the process of creating a continuous motion and shape change illusion by means of the rapid display of a sequence of static images ...

Anomalisa: Charlie Kaufman makes an animation for adults – sex scene included
The inventive Charlie Kaufman is behind one of 2015's most acclaimed films in the US.

Japan's Animation TV Ranking, January 11-17
Castle in the Sky, live-action Lupin III films air

Here's a 3-minute animation that will completely change the way you see the universe
Carl Sagan told us that there are more stars in the universe than there are grains of sand on all of the Earth's beaches. At the same time, there ...

‘Kung Fu Panda 3’ In China At $40M+ Through Saturday; Eyes Record Animation Debut
Kung Fu Panda 3 opened Friday in the Middle Kingdom, day-and-date with the U.S release, and industry estimates have it at 107M yuan, or $16.3M ...

With 'Kung Fu Panda 3' On Deck, The Five Most Underrated DreamWorks Animation Movies
'Monsters vs. Aliens,' image courtesy of DreamWorks Animation I thought I would usethe release of Kung Fu Panda 3 in theaters this week as an ...

Star Wars: The Force Awakens Summarized in a Hilariously Crude 60-Second Animation
If you haven’t seen Star Wars: The Force Awakens yet, why bother? Just troll the entire world by saying you were too busy or that Star Wars is ...

'The Wire' Animation Makes Us Want To Binge Watch It All Over Again
Fans of "The Wire," this one's for you. It's been eight long years since Omar Little, Stringer Bell and Jimmy McNulty left our TV screens. But ...

Resources last updated: 2/4/2016 2:11:32 PM