在 Matlab GUI 中将动画保存为 gif
Save animation as gif in Matlab GUI
我正在尝试将动画保存为 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
我想创建这个动画的 gif:
但它使用此函数“f=(handles.axes_threeDOF)”按给定 below 创建
我想我发现了问题:
f = handles.axes_threeDOF
获取 axes 的句柄,而不是获取 figure 的句柄。
由于不知道你的图名,没法给出完美的解决方案
您可以尝试以下选项:
1.Find figure 的名称,并使用类似于:f = handles.figure_threeDOF;
2.使用f = gcf();
,假设只有一个数字
3. 使用 f = handles.axes_threeDOF.Parent;
假设图形是坐标轴的 "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();
)的结果 - 获取坐标轴句柄而不是图形句柄:
我正在尝试将动画保存为 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
我想创建这个动画的 gif:
但它使用此函数“f=(handles.axes_threeDOF)”按给定 below 创建
我想我发现了问题:
f = handles.axes_threeDOF
获取 axes 的句柄,而不是获取 figure 的句柄。
由于不知道你的图名,没法给出完美的解决方案
您可以尝试以下选项:
1.Find figure 的名称,并使用类似于:f = handles.figure_threeDOF;
2.使用f = gcf();
,假设只有一个数字
3. 使用 f = handles.axes_threeDOF.Parent;
假设图形是坐标轴的 "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();
)的结果 - 获取坐标轴句柄而不是图形句柄: