You can use three basic techniques for creating animations in MATLAB®:
Update the properties of a graphics object and display the updates on the screen. This technique is useful for creating animations when most of the graph remains the same. For example, set the
YData properties repeatedly to move an object in the graph.
Apply transforms to objects. This technique is useful when you want to operate on the position and orientation of a group of objects together. Group the objects as children under a transform object. Create the transform object using
hgtransform. Setting the
Matrix property of the transform object adjusts the position of all its children.
Create a movie. Movies are useful if you have a complex animation that does not draw quickly in real time, or if you want to store an animation to replay it. Use the
movie functions to create a movie.
In some cases, MATLAB does not update the screen until the code finishes executing. Use one of the
drawnow commands to display the updates on the screen throughout the animation.
To optimize performance, consider these techniques:
animatedline function to create line animations of streaming data.
Update properties of an existing object instead of creating new graphics objects.
Set the axis limits (
ZLim) or change the associated mode properties to manual mode (
ZLimMode) so that MATLAB does not recalculate the values each time the screen updates. When you set the axis limits, the associated mode properties change to manual mode.
Avoid creating a legend or other annotations within a loop. Add the annotation after the loop.
For more information on optimizing performance, see Graphics Performance.