banner



How To Animate A Plot In Matlab

Animation

Table of Contents, Get code for this tutorial

Note: You tin can execute the code from this tutorial past highlighting them, right-clicking, and selecting "Evaluate Choice" (or hit F9).

In this tutorial, you will acquire how to do animations in MATLAB. You lot volition larn well-nigh creating animations of MATLAB graphs too as making AVI movies and animated GIFs.

Contents

  • Animating using Loops
  • Animative using Loops (Smart updating)
  • Animation using Timer Objects
  • Creating Movies (AVI)
  • Creating Blithe GIFs

Animating using Loops

You can easily create animations past using for loops (or while loops). At each loop iteration you can update the graph with the appropriate information for that time step.

thousand = 1;          L = ane;          theta1 = 3*pi/4;  theta2 = 3*pi/8;  t = linspace(0, ten, 300);     [T,Y] = ode45(@(t, x) double_pendulum(t, 10, m, Fifty),          ...          t, [theta1, theta2, 0, 0]);   10 = [ L*sin(Y(:,1)),  L*sin(Y(:,1))+L*sin(Y(:,2))]; y = [-50*cos(Y(:,1)), -L*cos(Y(:,ane))-50*cos(Y(:,2))];   ang = Y(:,1:2)*180/pi;  figure; subplot(2,1,1); xlabel('time (sec)'); ylabel('angle (\circ)');  tic;              for          id = ane:length(T)        subplot(2,1,i);    plot(T,ang,          'LineWidth', two);    line(T(id), ang(id,i),          'Mark',          '.',          'MarkerSize', twenty,          'Color',          'b');    line(T(id), ang(id,ii),          'Marker',          '.',          'MarkerSize', twenty,          'Color', [0 .five 0]);    xlabel('fourth dimension (sec)'); ylabel('bending (deg)');         subplot(ii,one,2);    plot([0, x(id,1);10(id,1), x(id,two)], [0, y(id,1);y(id,1), y(id,2)],          ...          '.-',          'MarkerSize', 20,          'LineWidth', 2);    axis          equal; axis([-2*50 ii*L -2*Fifty 2*L]);    title(sprintf('Fourth dimension: %0.2f sec', T(id)));     drawnow;          end          fprintf('Animation (Regular): %0.2f sec\n', toc);        
Animation (Regular): 9.02 sec        

Animating using Loops (Smart updating)

While it's perfectly fine to practice animations like above, in that location's a better way of doing blitheness that is more efficient in terms of retentiveness and speed. Some commands do a lot more than what the proper name suggests. For instance, plot may clear previous axes, remove existing graphics objects, reset some properties, and generate new graphics objects. Doing all of these actions every time through the loop may start to slow down the process peculiarly if y'all are animating large amounts of data.

A better arroyo is to do the main plotting once, and then simply modify the underlying data through the loop. To do that, you lot store the handles from the initial plotting routines, and use them to alter the XData and YData.

figure; subplot(2,1,ane); plot(T, ang,          'LineWidth', two); hh1(1) = line(T(1), ang(1,one),          'Marker',          '.',          'MarkerSize', xx,          'Color',          'b'); hh1(2) = line(T(1), ang(1,2),          'Mark',          '.',          'MarkerSize', xx,          'Colour', [0 .v 0]); xlabel('fourth dimension (sec)'); ylabel('bending (deg)');  subplot(2,one,ii); hh2 = plot([0, ten(1,1);x(1,i), x(1,two)], [0, y(1,one);y(1,one), y(one,2)],          ...          '.-',          'MarkerSize', 20,          'LineWidth', 2); axis          equal          centrality([-ii*L ii*Fifty -two*Fifty ii*Fifty]); ht = title(sprintf('Time: %0.2f sec', T(1)));  tic;               for          id = ane:length(T)        set(hh1(1),          'XData', T(id)          ,          'YData', ang(id, 1));    prepare(hh1(2),          'XData', T(id)          ,          'YData', ang(id, ii));    set(hh2(one),          'XData', [0, 10(id, 1)]  ,          'YData', [0, y(id, 1)]);    set(hh2(2),          'XData', ten(id, :)       ,          'YData', y(id, :));    set(ht,          'Cord', sprintf('Time: %0.2f sec', T(id)));     drawnow;          end          fprintf('Animation (Smart update): %0.2f sec\north', toc);        
Animation (Smart update): 3.22 sec        

If it's too fast, you lot tin can always put a pause command instead of drawnow.

Animation using Timer Objects

Instead of using loops, you can also create animations using MATLAB'southward timer objects. Timer objects are a generic way of performing scheduled actions. One of the advantages of using a timer object is that information technology allows you lot to do other operations in MATLAB in between timer executions. The documentation does a good chore explaining how to utilise timer objects. You basically put the graphics update code inside the timer function that gets executed every time interval.

Creating Movies (AVI)

Once you have an animation working in MATLAB, you tin easily create a movie file (AVI) using avifile or movie2avi.

            mov(1:length(T)) = struct('cdata', [],          'colormap', []);            for          id = 1:length(T)        set(hh1(1),          'XData', T(id)          ,          'YData', ang(id, one));    fix(hh1(2),          'XData', T(id)          ,          'YData', ang(id, two));    set(hh2(1),          'XData', [0, x(id, 1)]  ,          'YData', [0, y(id, one)]);    set(hh2(2),          'XData', x(id, :)       ,          'YData', y(id, :));    set(ht,          'String', sprintf('Time: %0.2f sec', T(id)));         mov(id) = getframe(gcf);          end           movie2avi(mov,          'blitheness.avi');  clear          mov        

Creating Animated GIFs

Another style to relieve the animation is to create an animated GIF. You tin can create ane using the imwrite office.

            pos = get(gcf,          'Position'); width = pos(3); height = pos(4);   mov = zeros(height, width, 1, length(T),          'uint8');            for          id = i:length(T)        set(hh1(1),          'XData', T(id)          ,          'YData', ang(id, 1));    set(hh1(ii),          'XData', T(id)          ,          'YData', ang(id, 2));    set(hh2(1),          'XData', [0, ten(id, 1)]  ,          'YData', [0, y(id, 1)]);    set(hh2(ii),          'XData', ten(id, :)       ,          'YData', y(id, :));    gear up(ht,          'String', sprintf('Time: %0.2f sec', T(id)));         f = getframe(gcf);                   if          id == 1       [mov(:,:,1,id), map] = rgb2ind(f.cdata, 256,          'nodither');          else          mov(:,:,i,id) = rgb2ind(f.cdata, map,          'nodither');          stop          end           imwrite(mov, map,          'animation.gif',          'DelayTime', 0,          'LoopCount', inf)        

Table of Contents

Source: http://web.mit.edu/8.13/matlab/MatlabTraining_IAP_2012/AGV/DemoFiles/ScriptFiles/html/Part3_Animation.html

Posted by: mcleodsallithere.blogspot.com

0 Response to "How To Animate A Plot In Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel