在Matlab GUI中将动画另存为gif

须贺

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

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

但是,不是使用“ figure”命令在单独的屏幕中绘制图形,而是必须在MATLAB GUI轴上绘制轴,如图所示。

我尝试使用: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:
在此处输入图片说明

But it creates as given below with this function " f=(handles.axes_threeDOF)" 在此处输入图片说明

Rotem

I think I found the problem:

f = handles.axes_threeDOF gets the handle of the axes instead of getting the handle of the figure.

Since I don't know the name of your figure, I can't give a perfect solution.
You may try the following options:

1.Find the name of the figure, and use something like: f = handles.figure_threeDOF;
2. Use f = gcf();, assuming there is only one figure.
3. Use f = handles.axes_threeDOF.Parent; assuming the figure is the "Parent" of the axes.


Update:

After im = frame2im(frame);, you need to crop the relevant part of the image:
Something like: 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();)-获取轴手柄而不是图形手柄:
在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在GUI Matlab中将另存为按钮

如何在MATLAB中将3D动画线另存为视频或gif

在Matlab中将单元格数组另存为.xlsx或.csv文件

我可以通过批处理在Matlab中将许多图形另存为位图吗?

如何以编程方式保存来自visdiff的MATLAB比较结果并将其重新加载到GUI或另存为HTML?

Matlab:将工作区变量另存为.txt文件

将图另存为音频文件Matlab

如何将画布动画另存为gif或webm?

MATLAB - 如何将动画函数保存为 gif

当图形另存为PDF时,为什么Matlab使用Latex在图例中放置文本空间?

如何将python字典列表另存为matlab结构化数组的数组?

MATLAB-如何从.fig文件中裁剪图像的某些部分并将其另存为.mat?

将Matlab图形另存为EPS文件时发生失真

MatLab-访问另存为变量的文件夹的子文件夹

在Matlab中创建表并将其另存为.mat文件

将刚体模拟另存为动画

如何在GIMP 2.8.16中将gif的图层另存为单独的文件?

在Windows中将文件另存为.RProfile

在 UWP 中将画布另存为图像

在Kotlin中将位图另存为图像

在MATLAB中另存为ascii时数字的格式始终是科学的吗?可以更改吗?

Matlab:如何将动画图保存为gif

Matlab动画

Matplotlib将单个“ ims”(动画部分)另存为图像

边缘另存为

如何在php中将json字节[]另存为pdf

如何在python中将列表另存为numpy数组?

如何在Chrome中将网页另存为MHT?

如何在Eclipse中将工作区另存为...?