我正在尝试将动画另存为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
但是它使用此函数“ f =(handles.axes_threeDOF)创建给定的below”
我认为我发现了问题:
f = handles.axes_threeDOF
获取axes的句柄,而不是获取figure的句柄。
由于我不知道你的名字,我无法给出完美的解决方案。您可以尝试以下选项:
1。找到图的名称,并使用类似以下的内容:f = handles.figure_threeDOF;
2.假设只有一个数字,请使用f = gcf();
。3.假设数字为轴的“父级”,请使用f = handles.axes_threeDOF.Parent;
。
这里是重现该问题的代码(而不是图形手柄,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 %%%
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