Creating Animations With Octave

Unlike MATLAB, the open source numerical package GNU Octave does not have support for quickly creating animations. That is quite disappointing, because as a physics student, I like to see what the time-dependent equations I solve look like. For example, I am currently taking a course with a section on boundary value problems. This includes the motion of waves on different surfaces, and following their evolution using Fourier series. As you can imagine, that might look quite cool.

Luckily, with the amount of tools available for GNU/Linux, I’ve managed to hack together a quick script that allows me to create AVI movies with Octave that I can then play back. It might not be as simple as MATLAB’s movie() function, but it works! Here is how:

Create a for loop, with each pass displaying the next frame in the animation.

Save each frame as an png (or some other type) image such that when the file names are ordered alphabetically, they are also ordered chronologically.

Use mencoder (or some other encoder) to stitch together the images into a movie file.

Line 1 hides the plot display, so it is not redrawn on the screen every time. This speeds up things considerably.

Line 19 sets the filename as “#####.png”, a 5 digits frame number. For example, if the frame number is 2, then the filename is 00002.png. This guarantees that the bash shell will order the filenames chronologically.

9 Comments

Thanks for the tips. It works!
I am using ffmpeg to create a video from png. There is a small
typo:
ffmpeg -i “%d5.png” -y output.mpeg
should be:
ffmpeg -i “%05d.png” -y output.mpeg
or else ffmpeg returns a confusing error, stating that the input
is corrupted.

I created a shell script to run the file.m and automatically run the mencoder command (thanx Ioredana for the typo tip, I am planning to use ffmpeg too). Scripts tend to be good ways of testing animations and similar repetitive tasks without retyping commands.