Open In App

How To Create Animated GIF Images in MATLAB?

Last Updated : 06 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The GIF stands for ( Graphics Interchanges format ). The purpose of the creation is GIFs images are dynamic. it’s a little bit difficult to understand, that’s why GIFs don’t allows them to show off details and motion and the other secret functionality which are hidden behind the GIFs image. that’s why it’s difficult to understand.

In MATLAB an animated GIF is an image Encoded in Graphics Interchange Format (GIF), which contains a large number of images or frames simultaneously in a single file and describes in its own way, its own graphics control extension. through MATLAB we create N number of an animated GIF image via writing efficient code.

So, Let’s take an example of how to create an animated GIF image in MATLAB with a step-by-step process-

Example 1:

Matlab




% Create a GIF animation image.
clear all
x = 0:0.01:1;
p = plot(nan,nan);
p.XData = x;
for n = 1:0.5:5
      p.YData = x.^n;
      exportgraphics(gcf,'testAnimated.gif','Append',true);
end
  
x = 0:0.01:1;
figure(1)
filename = 'testimage.gif';
for n = 1:0.5:5
      y = x.^n;
      plot(x,y)
      drawnow
      frame = getframe(1);
      im = frame2im(frame);
      [imind,cm] = rgb2ind(im,256);
      if n == 1;
          imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
      else
          imwrite(imind,cm,filename,'gif','WriteMode','append');
      end
end


 The above code will generate two outputs as you can see in the code the one filename is testAnimated.gif and another one is  testimage.gif.

Output : 

testimage.gif

testAnimated.gif

Example 2:

Matlab




% How to make GIF animation image.
clear all
close all 
clc 
x = [.1:0.02:1];
[X,Y] = meshgrid(x); 
  
% 2D grid
Z = X.^2*Y.^2; 
  
% papillon 
figure('Position', [200 200 600 600]) 
  
% Position and fixed size
surf(X,Y,Z)   
  
% magnitude of surface
shading('interp')
% delelte black grid


Output : 

 

Explanation: 

In MATLAB (%) percent sign is denoting the comment line in the code. in the above code, the term clear all and close all is basically used to clear/delete the variables created in Workspace.

[X,Y] = meshgrid() is used to return 2-D grid coordinates contained in vectors.  surf(X, Y, Z) it’s used to create a 3-dimensional surface plot. and clc it’s used in the command window to clear the commands.

When you write the code in the MATLAB editor and run then you will get that image(figure)

In the above-implemented code, figure() is used to fix the size and position and shading is used form remove the black grid from got the figure. after running this code you will get a new implement image which is shown below.

% Code
colormap('jet') % wow palette

In the above code, we just add one command in the last of the previous code colormap it’s a matrix of values that define objects, image, and graphics. then we will get a new GIF image.

Output : 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads