How to make GIF and Video of MATLAB simulations



Sometimes for the work you are doing or for the simulations you are running, you need to show the progression of the simulation.

In such cases, one approach is to save each plot from the simulation timeframe individually and then stitch them together in another software or website to get the final animation in gif or video format.

Matlab allows you to do it in the code within the Matlab environment.

Here is the sample code for obtaining a video in either gif or video (avi) format from the results of your simulation.

clc; % clear workspace clear; % clear command window framei = 0; % stores frame index max_val = 30; % stores maximum value for the simulation close all % close all open figures set(gcf, 'Units', 'Normalized', 'OuterPosition', [0.2, 0.2, 0.6, 0.7]); % set figure size set(gcf, 'Color', 'w') %set the figure background to white instead of default grey for i=1:0.5:max_val % start looping through simulation x=0:0.01:i; % define x values y=sin(x); % define y values plot(x,y,'linewidth',2) % plot x vs. y xlabel(['X value ' 'Max=' num2str(i,'%2.2f')]) % add x label ylabel('Y value') % add y lable xlim([0,max_val]) % set x asis limits ylim([-1,1]) % set y axis limits grid on % add girdlines set(findall(gcf,'-property','FontSize'),'FontSize',20) %set font size to 20 set(findall(gcf,'-property','FontName'),'FontName','Times') %set font name to Times ax = gca; % get current axis ti = ax.TightInset; % get current axis tight bounds ax.Position = [ti(1), ti(2), 1 - ti(1) - ti(3), 1 - ti(2) - ti(4)]; % remove extra empty marging around the axis drawnow % this is required to show the figure and create graphis for proper frame storage framei = framei+ 1; % increase the frame counter F(framei)=getframe(gcf); % capture the plot as an image and store frames in an array end %% store animation as gif file filename = 'script_animated.gif'; % set gif filename for i=1:framei im = frame2im(F(i)); % convert frames to images [imind,cm] = rgb2ind(im,256); % extract image data to write in gif if (i == 1) % write to the GIF File imwrite(imind,cm,filename,'gif','DelayTime',0.1, 'Loopcount',inf); % for first frame only else imwrite(imind,cm,filename,'gif','DelayTime',0.1,'WriteMode','append'); % for other frames end end %% store animation as video (avi) file video = VideoWriter('script_animated.avi'); % create the video writer video.FrameRate = 10; % set the images/frames per second open(video); % open the video writer for i=1:framei % write the frames to the video writeVideo(video, F(i)); % convert the image to a frame end close(video); % close the writer object {codeBox}


This would result in a gif animation as follows (Example 1)


and video animation in the avi format.

The full code, data, and sample animation outputs for this tutorial (Example 1) can be found below

{getButton} $text={Download the code package} $icon={download}

The full code, data, and sample animation outputs for this tutorial (Square signal example) can be found below

{getButton} $text={Download the code package} $icon={download} $color={red}



Post a Comment

Previous Post Next Post