MATLAB-如何将动画函数保存为 gif
MATLAB- How o save an animated function as a gif
我正在使用名为 "animated line" 的函数创建一些动画函数。
我想了解如何将我的功能保存为 GIF 以在其他地方使用它,例如微软幻灯片软件。
我正在尝试使用 gif function 但我不能。谁能建议我如何实现它或如何正确使用 gif 功能?
这里有一个简单的代码:
numpoints = 100000;
x = linspace(0,4*pi,numpoints);
y = square(x);
y2 = 3 +square(x+1);
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
end
使用imwrite
函数创建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 保存到名为 test.gif 的文件中。
我正在使用名为 "animated line" 的函数创建一些动画函数。 我想了解如何将我的功能保存为 GIF 以在其他地方使用它,例如微软幻灯片软件。 我正在尝试使用 gif function 但我不能。谁能建议我如何实现它或如何正确使用 gif 功能? 这里有一个简单的代码:
numpoints = 100000;
x = linspace(0,4*pi,numpoints);
y = square(x);
y2 = 3 +square(x+1);
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
end
使用imwrite
函数创建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 保存到名为 test.gif 的文件中。