我正在尝试将动画另存为GIF动画。
我的情节类似于下面的给定代码。
我也用动画线创建了它。
问题是:
当我将图形定义为f=figure
或figure(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
But it creates as given below with this function " f=(handles.axes_threeDOF)"
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句