MATLAB使用绘图plot制作动态GIF

文章目录

  • [1 前言](#1 前言)
  • [2 Demo](#2 Demo)
    • [Demo 1 - 不使用函数](#Demo 1 - 不使用函数)
    • [Demo 2 - 使用函数](#Demo 2 - 使用函数)

1 前言

在PPT展示或者博客创作中,有时需要插入动态图如GIF,来演示算法效果或者结果。在MATLAB中,可以通过一些代码,将绘图plot转化为动态的GIF。

其大致方法为,写一个循环,每个循环为一个帧,绘制该帧的内容,将此时figure的内容转化为一帧图像,定义两帧的间隔,然后添加到GIF中(第一帧和其他帧的处理方式不一样,详见代码)。

如果想将GIF插入到CSDN博客中,只需要点击工具栏中的图片,然后添加即可。

效果如下:

2 Demo

这里提供两个demo。

Demo 1 - 不使用函数

需要一个文件,create_gif_demo_1.m

需要修改的只有两处:

  • 文件名 。这里文件名为animation.gif,注意不要漏了后缀。这样会在当前目录下,生成GIF文件。
  • 帧间隔。这里设置的时间间隔为0.1s,可以根据需要修改。
matlab 复制代码
clear all
clc

% Define the time steps for your animation
timeSteps = 0:0.1:2*pi;

% Create a figure handle
figure;

% Loop through each time step
for i = 1:length(timeSteps)
    % Generate your plot for each time step
    x = linspace(0, 2*pi, 100);
    y = sin(x + timeSteps(i));
    
    % Plot customization
    plot(x, y, 'LineWidth', 2);
    title('Sinusoidal Wave Animation');
    xlabel('X-axis');
    ylabel('Y-axis');
    
    % Capture the current frame
    frame = getframe(gcf);
    
    % Convert the frame to an image matrix
    img = frame2im(frame);
    [A,map] = rgb2ind(img,256);
    % Save the image as a GIF (adjust filename as needed)
    if i == 1
        imwrite(A,map, 'animation.gif', 'gif', 'Loopcount', inf, 'DelayTime', 0.1);
    else
        imwrite(A,map, 'animation.gif', 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
    end
    
	% Pause to create a smooth animation
	pause(0.1);% For real-time viewing only, can be removed
end

% Display a message indicating successful GIF creation
disp('GIF animation created successfully.');

Demo 2 - 使用函数

需要两个文件(放在同一目录下):create_gif.mcreate_gif_demo_2.m

不同于demo 1的地方在于,这里把和GIF相关的代码封装进一个函数里。

这样只需要传入参数:是否为第一帧的flag,帧与帧的时间间隔,GIF文件名称。

matlab 复制代码
function [] = create_gif(flag,delay_time,name_of_the_file)
    % 
    % Capture the current frame
    frame = getframe(gcf);
    
    % Convert the frame to an image matrix
    img = frame2im(frame);
    [A,map] = rgb2ind(img,256);
    
    % Save the image as a GIF (adjust filename as needed)
    if flag == 1 % flag == 1 means it's the first frame
        imwrite(A,map, name_of_the_file, 'gif', 'Loopcount', inf, 'DelayTime', delay_time);
    else
        imwrite(A,map, name_of_the_file, 'gif', 'WriteMode', 'append', 'DelayTime', delay_time);
    end
end
matlab 复制代码
clear all
clc

delay = 0.1; % delay between frames, 0.1s
file_name = 'gif_demo_2.gif'; % file name, careful about the .gif suffix

% Define the time steps for your animation
timeSteps = 0:0.1:2*pi;

% Create a figure handle
figure;

% Loop through each time step
for i = 1:length(timeSteps)
    % Generate your plot for each time step
    x = linspace(0, 2*pi, 100);
    y = sin(x + timeSteps(i));
    
    % Plot customization
    plot(x, y, 'LineWidth', 2);
    title('Sinusoidal Wave Animation');
    xlabel('X-axis');
    ylabel('Y-axis');
    
    % Create GIF
    flag = i; % flag to indicate whether it's the first frame
    create_gif(flag, delay, file_name);
    
	% Pause to create a smooth animation
	pause(0.1);% For real-time viewing only, can be removed
end

% Display a message indicating successful GIF creation
disp('GIF animation created successfully.');
相关推荐
yugi9878386 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
IT猿手15 小时前
基于强化学习的多算子差分进化路径规划算法QSMODE的机器人路径规划问题研究,提供MATLAB代码
算法·matlab·机器人
fie888919 小时前
基于MATLAB的转子动力学建模与仿真实现(含碰摩、不平衡激励)
开发语言·算法·matlab
机器学习之心19 小时前
基于GRU门控循环单元的轴承剩余寿命预测MATLAB实现
深度学习·matlab·gru·轴承剩余寿命预测
简简单单做算法20 小时前
基于FFT粗估计和LS最小二乘法精估计的正弦信号参数估计和检测matlab仿真
matlab·最小二乘法·参数估计·fft粗估计·ls最小二乘法
kaikaile199520 小时前
基于MATLAB的滑动轴承弹流润滑仿真程序实现
开发语言·matlab
Not Dr.Wang4221 天前
FIR数字滤波器设计的两种实现
matlab
3GPP仿真实验室1 天前
【MATLAB源码】CORDIC-QR :基于Cordic硬件级矩阵QR分解
开发语言·matlab·矩阵
民乐团扒谱机2 天前
【微实验】机器学习之集成学习 GBDT和XGBoost 附 matlab仿真代码 复制即可运行
人工智能·机器学习·matlab·集成学习·xgboost·gbdt·梯度提升树
Evand J2 天前
TDOA(到达时间差)的GDOP和CRLB计算的MATLAB例程,论文复现,附参考文献。GDOP:几何精度因子&CRLB:克拉美罗下界
开发语言·matlab·tdoa·crlb·gdop