• MATLAB使用绘图plot制作动态GIF


    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,可以根据需要修改。
    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.');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    Demo 2 - 使用函数

    需要两个文件(放在同一目录下):create_gif.mcreate_gif_demo_2.m
    不同于demo 1的地方在于,这里把和GIF相关的代码封装进一个函数里。
    这样只需要传入参数:是否为第一帧的flag,帧与帧的时间间隔,GIF文件名称。

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    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.');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    如何用神经网络预测数据,神经网络预测的软件有
    基于双向LSTM模型进行电力需求预测(Matlab代码实现)
    运维自动导出业务容器Java堆栈错误日志脚本
    关于使用docker volume挂载的注意事项
    pycharm 包安装失败,换源下载,一行命令
    Python+Selenium安装及环境配置手把手教会你
    Redisson入坑篇
    CAS号:81075-03-8,H2N-AYA-OH
    「数据结构」哈希表2:实现哈希表
    HTML5生日快乐在线网页祝福 (一场浪漫的烟花秀) HTML+CSS+JavaScript
  • 原文地址:https://blog.csdn.net/weixin_45910027/article/details/136255206