【问题标题】:Save animation as gif in Matlab GUI在 Matlab GUI 中将动画保存为 gif
【发布时间】:2020-02-27 01:40:01
【问题描述】:

我正在尝试将动画保存为动画 GIF。
我的情节类似于下面给定的代码。
我也用动画线创建了它。

问题是:
当我将我的图形定义为 f=figurefigure(1) 时,它会正确创建 .gif 文件。

但是,我必须在 MATLAB GUI 轴上绘制一个轴作为给定的图形,而不是使用“figure”命令在单独的屏幕上绘制我的图形。

我尝试过:f=(handles.axes_threeDOF);,但是当我使用此功能时,gif 文件会创建屏幕的不同部分。

你能帮我解决我的问题吗?

numpoints = 500; 

x = linspace(0,4*pi,numpoints); 

y = square(x); 

y2 = 3 +square(x+1);

f = figure 

h = animatedline('Color','b','LineWidth',2); 

h2 = animatedline('Color','r','LineWidth',2);

grid on;

axis([0,12,-3,+6]) 

for k = 1:numpoints 

  addpoints(h,x(k),y(k)) 

  addpoints(h2,x(k),y2(k)) 

  drawnow  

  % Capture the plot as an image 

  frame = getframe(f); 

  im = frame2im(frame); 

  [imind,cm] = rgb2ind(im,256); 

  % Write to the GIF File 

  if k == 1 

      imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf); 

  else 

      imwrite(imind,cm,'test.gif','gif','WriteMode','append'); 

  end 

end

我想制作这个动画的 gif:

但它使用这个函数“f=(handles.axes_threeDOF)”创建给定的below

【问题讨论】:

    标签: matlab user-interface plot save gif


    【解决方案1】:

    我想我发现了问题:

    f = handles.axes_threeDOF 获取 axes 的句柄,而不是获取 figure 的句柄。

    由于不知道你的图名,所以无法给出完美的解决方案。
    您可以尝试以下选项:

    1.找到人物的名称,并使用类似:f = handles.figure_threeDOF;
    2.使用f = gcf();,假设只有一个数字。
    3. 使用f = handles.axes_threeDOF.Parent; 假设图形是轴的“父级”。


    更新:

    im = frame2im(frame);之后,需要裁剪图片的相关部分:
    比如:im = im(36:884, 928:1800, :);

    有比使用固定索引更强大的解决方案,但它需要一些关于图形内部结构的知识。


    这是重现问题的代码(而不是图形句柄,f 获取轴句柄):

    numpoints = 500;
    x = linspace(0,4*pi,numpoints);
    y = square(x);
    y2 = 3 +square(x+1);
    f = figure;
    h = animatedline('Color','b','LineWidth',2);
    h2 = animatedline('Color','r','LineWidth',2);
    grid on;
    axis([0,12,-3,+6])
    
    for k = 1:numpoints
        addpoints(h,x(k),y(k))
        addpoints(h2,x(k),y2(k))
        drawnow
    
        %%% Test %%%
        %The following code demonstrates the problem.
        %f = gca(), returns a handle to the axes, instead of the figure.
        %You should remove the following line for the code to work properly... 
        f = gca();
        %%% Test %%%
    
        % Capture the plot as an image
        frame = getframe(f);
        im = frame2im(frame);
        [imind,cm] = rgb2ind(im,256);
    
        % Write to the GIF File
        if k == 1
            imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf); 
        else
            imwrite(imind,cm,'test.gif','gif','WriteMode','append'); 
        end
    end
    

    正确代码的结果(没有f = gca();):

    错误代码的结果(使用f = gca();) - 获取坐标轴句柄而不是图形句柄:

    【讨论】:

    • 嗨罗特姆!谢谢您的回答。当我将 f=figure 和 gca() 添加到我的代码中时,它仍然会在不同的窗口中创建我的图。我想在我的 GUI 轴(axes_threeDOF)中看到这个数字。实际上,我可以使用 f=(handles.axes_threeDOF) 在 GUI 轴上绘制它。问题是将其导出为 gif。它不会像那样保存整个轴ibb.co/MnTR9fc
    • 我想你没听懂我的回答。使用gca() 的代码重现了您的问题。使用gca()是错误的!!!
    • 你试过f = gcf();吗?你试过f = handles.axes_threeDOF.Parent吗?
    • 我编辑了我的帖子,只是为了表明f = gca(); 是一个“错误”。 (我添加了“错误”来演示问题)。我还添加了两个结果,因此您可以看到 f = gca(); 给出的结果与您得到的结果相似。
    • 不是“全屏”,而是“全图”。如果将图形最大化到“整个屏幕”,您将获得整个屏幕......我认为临时解决方案可以裁剪图像的相关部分。比如:im = im(36:884,928:1800,:);
    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 2021-04-25
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2018-11-13
    • 2014-02-14
    相关资源
    最近更新 更多