This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

writeAnimation

Save animation as video file

Syntax

writeAnimation(filename)
writeAnimation(fig,filename)
writeAnimation(___,Name,Value)
writeAnimation(vidObj)
writeAnimation(fig,vidObj)

Description

example

writeAnimation(filename) writes animation objects in the current figure to a GIF or AVI video file. The animation objects must be created using the fanimator function. The extension of filename sets the video format, and must be either '.gif' or '.avi'.

  • If you do not specify the file extension, then writeAnimation chooses the '.avi' extension by default.

  • If you specify any other file extension, such as '.mp4' or '.mpg', then writeAnimation returns an error message.

example

writeAnimation(fig,filename) writes animation objects in the figure fig to a GIF or AVI video file.

example

writeAnimation(___,Name,Value) writes animation objects with the specified Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes. You can set the name-value pair settings to specify the properties of a GIF or AVI video file.

example

writeAnimation(vidObj) writes animation objects in the current figure to a VideoWriter object. This syntax provides the option to save animation objects in another video file format, such as 'MPEG-4' or 'Uncompressed AVI'.

writeAnimation(fig,vidObj) writes animation objects in the figure fig to a VideoWriter object.

If you save an animation as a VideoWriter object, then the properties of the output video file follow the properties of the VideoWriter object.

Examples

collapse all

Create a moving circle animation object and save it as a GIF file.

Create two symbolic variables, t and x. The variable t defines the time parameter of the animation. Use t to set the center of the circle at (t,1) and x to parameterize the perimeter of the circle within the range [-pi pi]. Create the circle animation object using fanimator. Set the x-axis and y-axis to be equal length.

syms t x
fanimator(@fplot,cos(x)+t,sin(x)+1,[-pi pi])
axis equal

Enter the command playAnimation to play the animation. Save the animation as a GIF video file named 'wheel.gif'.

writeAnimation('wheel.gif')

Create a moving circle animation object and save it as an MPEG-4 file.

First, create two symbolic variables, t and x. The variable t defines the time parameter of the animation. Use t to set the center of the circle at (t,1) and x to parameterize the perimeter of the circle within the range [-pi pi]. Create the circle animation object using fanimator. Set the x-axis and y-axis to be equal length.

syms t x
fanimator(@fplot,cos(x)+t,sin(x)+1,[-pi pi])
axis equal

Enter the command playAnimation to play the animation.

Next, save the animation as an MPEG-4 file. Create a video object that saves to a file named 'myFile' by using the VideoWriter function. Specify the video file format as 'MPEG-4'. Open the video file, use writeAnimation to save the circle animation object, and close the video file.

vidObj = VideoWriter('myFile','MPEG-4');
open(vidObj)
writeAnimation(vidObj)
close(vidObj)

Create a circle animation object and save it as a GIF file that plays a specified number of loops.

First, create two symbolic variables, t and x. The variable t defines the time parameter of the animation. Create a figure window for the animation.

syms t x
fig = figure;

Create the circle animation object using fanimator. Use t to set the center of the circle at (t,1) and x to parameterize the perimeter of the circle within the range [-pi pi]. Set the x-axis and y-axis to be equal length.

syms t x
fanimator(@fplot,cos(x)+t,sin(x)+1,[-pi pi])
axis equal

Enter the command playAnimation to play the animation.

Next, save the animation in the figure fig as a GIF file named 'loop.gif' by using the writeAnimation function. The writeAnimation function always plays the animation once in a MATLAB® figure window before saving the animation. When saving the animation as a GIF file, the created GIF file plays the animation once and repeats the number of loops as specified. For this example, set 'LoopCount' to 1. The GIF file plays the animation twice.

writeAnimation(fig,'loop.gif','LoopCount',1)

Note that to properly show the number of loops in a GIF video file, you must open the file in an application with GIF decoder.

Input Arguments

collapse all

Video filename, specified as a string scalar or character vector. The extension of filename sets the video format, and must be either '.gif' or '.avi'. You must have permission to write the file.

  • If you do not specify the file extension, then writeAnimation uses '.avi' by default.

  • If filename already exists, then writeAnimation overwrites the file.

  • If filename does not include a full path, then the function saves the animation to the current folder.

Video object, specified as a VideoWriter object. The VideoWriter object provides the option to control the output video format when you save animation objects. For more information about VideoWriter object in MATLAB, see VideoWriter.

Target figure, specified as a Figure object. For more information about Figure objects, see figure.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'FrameRate',15,'LoopCount',2

Range of the animation time parameter, specified as a two-element row vector. The two elements must be real values that are increasing.

Example: [-2 4.5]

Frame rate, specified as a positive value. The frame rate defines the number of frames per unit time when you write animation objects to a video file.

Example: 30

Backward option, specified as a logical value (boolean). If you specify true, then the function saves the animation backwards or in reverse order.

Example: true

Animation loop count, specified as a nonnegative integer (from 0 to 65535) or Inf. This value sets the number of repeated animation loops in a GIF file. Setting this value has no effect if you use a video file format other than GIF.

  • If you use the default value of 0, then the GIF file plays the animation once.

  • If you set 'LoopCount' to an integer n, then the GIF file plays the animation once plus n repeats (a total of n+1 times).

  • To repeat the animation infinitely, use the Inf value.

Example: 1

Introduced in R2019a