Create animated line - MATLAB animatedline (2024)

Create animated line

collapse all in page

Syntax

an = animatedline

an = animatedline(x,y)

an = animatedline(x,y,z)

an = animatedline(___,Name,Value)

an = animatedline(ax,___)

Description

an = animatedline creates an animated line that has no data and adds it to the current axes. Create an animation by adding points to the line in a loop using the addpoints function.

example

an = animatedline(x,y) creates an animated line with initial data points defined by x and y.

an = animatedline(x,y,z) creates an animated line with initial data points defined by x, y, and z.

example

an = animatedline(___,Name,Value) specifies animated line properties using one or more name-value pair arguments. For example, 'Color','r' sets the line color to red. Use this option after any of the input argument combinations in the previous syntaxes.

an = animatedline(ax,___) creates the line in the axes specified by ax instead of in the current axes. Specify ax before all other input arguments in any of the previous syntaxes.

Examples

collapse all

Display Line Animation

Open Script

Create the initial animated line object. Then, use a loop to add 1,000 points to the line. After adding each new point, use drawnow to display the new point on the screen.

h = animatedline;axis([0,4*pi,-1,1])x = linspace(0,4*pi,1000);y = sin(x);for k = 1:length(x) addpoints(h,x(k),y(k)); drawnowend

Create animated line - MATLAB animatedline (1)

For faster rendering, add more than one point to the line each time through the loop or use drawnow limitrate.

Query the points of the line.

[xdata,ydata] = getpoints(h);

Clear the points from the line.

clearpoints(h)drawnow

Create animated line - MATLAB animatedline (2)

Specify Animated Line Color

Open Live Script

Set the color of the animated line to red and set its line width to 3 points.

Create animated line - MATLAB animatedline (3)

Specify Datetime and Duration Values

Open Live Script

To plot nonnumeric points, such as datetime and duration values, start by initializing the animated line with values of the type you want to plot. You can specify either the first point in your plot or placeholder values such as NaT or NaN.

For example, plot datetime values on the x-axis and duration values (minutes) on the y-axis. Initialize the animated line with a NaT value and a minutes(NaN) value. Then create a datetime vector (x) and a duration vector (y) and add the points in those vectors to the animated line.

an = animatedline(NaT,minutes(NaN),"Marker","o");x = datetime(2018,5,1:5);y = minutes([1 7 3 11 4]);addpoints(an,x,y)

Create animated line - MATLAB animatedline (4)

Set Maximum Number of Points

Open Script

Limit the number of points in the animated line to 100. Use a loop to add one point to the line at a time. When the line contains 100 points, adding a new point to the line deletes the oldest point.

h = animatedline('MaximumNumPoints',100);axis([0,4*pi,-1,1])x = linspace(0,4*pi,1000);y = sin(x);for k = 1:length(x) addpoints(h,x(k),y(k)); drawnowend

Create animated line - MATLAB animatedline (5)

Add Points in Sets for Fast Animation

Open Script

Use a loop to add 100,000 points to an animated line. Since the number of points is large, adding one point to the line each time through the loop might be slow. Instead, add 100 points to the line each time through the loop for a faster animation.

h = animatedline;axis([0,4*pi,-1,1])numpoints = 100000;x = linspace(0,4*pi,numpoints);y = sin(x);for k = 1:100:numpoints-99 xvec = x(k:k+99); yvec = y(k:k+99); addpoints(h,xvec,yvec) drawnowend

Create animated line - MATLAB animatedline (6)

Another technique for creating faster animations is to use drawnow limitrate instead of drawnow.

Use drawnow limitrate for Fast Animation

Open Script

Use a loop to add 100,000 points to an animated line. Since the number of points is large, using drawnow to display the changes might be slow. Instead, use drawnow limitrate for a faster animation.

h = animatedline;axis([0,4*pi,-1,1])numpoints = 100000;x = linspace(0,4*pi,numpoints);y = sin(x);for k = 1:numpoints addpoints(h,x(k),y(k)) drawnow limitrateend

Create animated line - MATLAB animatedline (7)

Time Screen Updates for Efficiency

Open Script

Run through several iterations of the animation loop before drawing the updates on the screen. Use this technique when drawnow is too slow and drawnow limitrate is too fast.

For example, update the screen every 1/30 seconds. Use the tic and toc commands to keep track of how much time passes between screen updates.

h = animatedline;axis([0,4*pi,-1,1])numpoints = 10000;x = linspace(0,4*pi,numpoints);y = sin(x);a = tic; % start timerfor k = 1:numpoints addpoints(h,x(k),y(k)) b = toc(a); % check timer if b > (1/30) drawnow % update screen every 1/30 seconds a = tic; % reset timer after updating endenddrawnow % draw final frame

Create animated line - MATLAB animatedline (8)

A smaller interval updates the screen more often and results in a slower animation. For example, use b > (1/1000) to slow down the animation.

Input Arguments

collapse all

xStarting x-coordinate
[] (default) | scalar or vector

Starting x-coordinate, specified as a scalar or vector the same size as y.

In polar coordinates, x corresponds to the starting theta value. In geographic coordinates, x corresponds to the starting latitude in degrees.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | datetime | duration

yStarting y-coordinate
[] (default) | scalar or vector

Starting y-coordinate, specified as a scalar or vector the same size as x.

In polar coordinates, y corresponds to the starting radius value. In geographic coordinates, y corresponds to the starting longitude in degrees.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | datetime | duration

zStarting z-coordinate
[] (default) | scalar or vector

Starting z-coordinate, specified as a scalar or vector.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | datetime | duration

axTarget axes
any type of axes | Group object | Transform object

Target axes, specified as any type of axes, a Group object, or a Transform object. If you do not specify this argument, then animatedline uses the current axes.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: animatedline(x,y,Color="red",Marker="o") creates an animated line with red circular markers.

Before R2021a: use commas to separate each name and value, and enclose Name in quotes. for example, animatedline(x,y,"Color","red","Marker","o") creates an animated line with red circular markers.

The animated line properties listed here are only a subset. For a complete list, see AnimatedLine Properties.

MarkerEdgeColorMarker outline color
'auto' (default) | RGB triplet | hexadecimal color code | 'r' | 'g' | 'b'

Marker outline color, specified as "auto", an RGB triplet, a hexadecimal color code, a color name, or a short name. The default value of "auto" uses the same color as the Color property.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Create animated line - MATLAB animatedline (9)

"green""g"[0 1 0]"#00FF00"

Create animated line - MATLAB animatedline (10)

"blue""b"[0 0 1]"#0000FF"

Create animated line - MATLAB animatedline (11)

"cyan" "c"[0 1 1]"#00FFFF"

Create animated line - MATLAB animatedline (12)

"magenta""m"[1 0 1]"#FF00FF"

Create animated line - MATLAB animatedline (13)

"yellow""y"[1 1 0]"#FFFF00"

Create animated line - MATLAB animatedline (14)

"black""k"[0 0 0]"#000000"

Create animated line - MATLAB animatedline (15)

"white""w"[1 1 1]"#FFFFFF"

Create animated line - MATLAB animatedline (16)

"none"Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Create animated line - MATLAB animatedline (17)

[0.8500 0.3250 0.0980]"#D95319"

Create animated line - MATLAB animatedline (18)

[0.9290 0.6940 0.1250]"#EDB120"

Create animated line - MATLAB animatedline (19)

[0.4940 0.1840 0.5560]"#7E2F8E"

Create animated line - MATLAB animatedline (20)

[0.4660 0.6740 0.1880]"#77AC30"

Create animated line - MATLAB animatedline (21)

[0.3010 0.7450 0.9330]"#4DBEEE"

Create animated line - MATLAB animatedline (22)

[0.6350 0.0780 0.1840]"#A2142F"

Create animated line - MATLAB animatedline (23)

MarkerFaceColorMarker fill color
'none' (default) | 'auto' | RGB triplet | hexadecimal color code | 'r' | 'g' | 'b'

Marker fill color, specified as "auto", an RGB triplet, a hexadecimal color code, a color name, or a short name. The "auto" option uses the same color as the Color property of the parent axes. If you specify "auto" and the axes plot box is invisible, the marker fill color is the color of the figure.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Create animated line - MATLAB animatedline (24)

"green""g"[0 1 0]"#00FF00"

Create animated line - MATLAB animatedline (25)

"blue""b"[0 0 1]"#0000FF"

Create animated line - MATLAB animatedline (26)

"cyan" "c"[0 1 1]"#00FFFF"

Create animated line - MATLAB animatedline (27)

"magenta""m"[1 0 1]"#FF00FF"

Create animated line - MATLAB animatedline (28)

"yellow""y"[1 1 0]"#FFFF00"

Create animated line - MATLAB animatedline (29)

"black""k"[0 0 0]"#000000"

Create animated line - MATLAB animatedline (30)

"white""w"[1 1 1]"#FFFFFF"

Create animated line - MATLAB animatedline (31)

"none"Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Create animated line - MATLAB animatedline (32)

[0.8500 0.3250 0.0980]"#D95319"

Create animated line - MATLAB animatedline (33)

[0.9290 0.6940 0.1250]"#EDB120"

Create animated line - MATLAB animatedline (34)

[0.4940 0.1840 0.5560]"#7E2F8E"

Create animated line - MATLAB animatedline (35)

[0.4660 0.6740 0.1880]"#77AC30"

Create animated line - MATLAB animatedline (36)

[0.3010 0.7450 0.9330]"#4DBEEE"

Create animated line - MATLAB animatedline (37)

[0.6350 0.0780 0.1840]"#A2142F"

Create animated line - MATLAB animatedline (38)

Output Arguments

collapse all

anAnimatedLine object
AnimatedLine object

AnimatedLine object. Use an to modify the AnimatedLine object after its been created, such as changing property values or adding points to the line. For a list of properties, see AnimatedLine Properties.

Limitations

Animated lines do not support data tips.

Extended Capabilities

Version History

Introduced in R2014b

expand all

Create animated lines using single, double, integer, datetime, or duration data for the x-, y-, and z-coordinates.

See Also

Functions

  • addpoints | clearpoints | getpoints

Properties

  • AnimatedLine Properties

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Create animated line - MATLAB animatedline (39)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Create animated line - MATLAB animatedline (2024)

FAQs

Create animated line - MATLAB animatedline? ›

an = animatedline creates an animated line that has no data and adds it to the current axes. Create an animation by adding points to the line in a loop using the addpoints function. an = animatedline( x , y ) creates an animated line with initial data points defined by x and y .

How to make animation using MATLAB? ›

For Loops to Create animations in Matlab

On each callback, the drawnow command refreshes the figures. To create an animated plot, use it within a loop to plot one variable in one iteration and refresh the figure with the drawnow command. There are several examples and tasks you can use to create an animated plot.

What is drawnow in MATLAB? ›

drawnow updates figures and processes any pending callbacks. Use this command if you modify graphics objects and want to see the updates on the screen immediately.

What is a comet graph? ›

A comet plot is an animated graph in which a circle (the comet head) traces the data points on the screen. The comet body is a trailing segment that follows the head. The tail is a solid line that traces the entire function.

How to export MATLAB animation as video? ›

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' .

How do you make an animated line in MATLAB? ›

an = animatedline creates an animated line that has no data and adds it to the current axes. Create an animation by adding points to the line in a loop using the addpoints function. an = animatedline( x , y ) creates an animated line with initial data points defined by x and y .

What is simulate in MATLAB? ›

Simulation is used to evaluate a new design, diagnose problems with an existing design, and test a system under conditions that are hard to reproduce in an actual system.

What is Ginput in MATLAB? ›

[ x , y ] = ginput( n ) allows you to identify the coordinates of n points within Cartesian, polar, or geographic axes. To choose a point, move your cursor to the desired location and press either a mouse button or a key on the keyboard. Press the Return key to stop before all n points are selected.

What is the use of Simscape in MATLAB? ›

Simscape™ enables you to rapidly create models of physical systems within the Simulink® environment. With Simscape you build physical component models based on physical connections that directly integrate with block diagrams and other modeling paradigms.

What is a hyperbolic comet? ›

Hyperbolic comet, a Solar System comet with an eccentricity greater than 1. Interstellar object – Astronomical object not gravitationally bound to a star.

What is the comet that comes every 76 years? ›

Halley was last seen in Earth's skies in 1986 and was met in space by an international fleet of spacecraft. It will return in 2061 on its regular 76-year journey around the Sun.

How does NASA track comets? ›

Spacecraft and Earth-based observations monitor and track asteroids and comets, most of which are too small or too distant to pose any danger to us. Scientists do keep extra-careful watch on those that approach a bit too close for comfort.

Can MATLAB do animations? ›

You can use three basic techniques for creating animations in MATLAB®. This example shows how to trace a marker along a line by updating the data properties of the marker. This example shows how to create an animation of two growing lines.

How do I record an animated plot in MATLAB? ›

Record and Play Back Movie

Create a series of plots within a loop and capture each plot as a frame. Ensure the axis limits stay constant by setting them each time through the loop. Store the frames in M . Play back the movie five times using the movie function.

What is a VideoWriter in MATLAB? ›

Use a VideoWriter object to create a video file from an array or MATLAB® movie. The object contains information about the video and the properties that control the output video. You can create a VideoWriter object using the VideoWriter function, specify its properties, and then write the video using object functions.

How to create visuals in MATLAB? ›

Visualize Data with MATLAB
  1. Click Apps > MATLAB Visualizations.
  2. Click New to start your visualization.
  3. Select a template or an example with sample code, which you can run and explore the results.
  4. Click Create.

How do I actually produce an animation? ›

There are 8 key stages of the animation process steps that you need to pay attention to:
  1. Researching.
  2. Defining the project's scope.
  3. Writing the script.
  4. Voice recording.
  5. Storyboarding.
  6. Illustration.
  7. Animation.
  8. Sound design.
Sep 23, 2022

How to make MATLAB animation faster? ›

If you want the animation to be even quicker then just plot every 2nd, 3rd, ..., nth timestep instead of every timestep by using for k = 2:n:maxT .

Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6704

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.