考虑下面的函数,该函数绘制了一个动画的cicloid图。
function animate1()
clear, clc
R = 1;
na = -pi/2;
t = 0:0.05:6;
v = 4;
for i = 1:length(t)
x0 = v*t(i);
y0 = R;
na = -v*t(i)/R;
fi = linspace(na,na+2*pi,100);
x = x0 + R*cos(fi);
y = y0 + R*sin(fi);
xc(i) = x0 + R*cos(na);
yc(i) = y0 + R*sin(na);
plot(x,y,'b',...
xc(i),yc(i),'*m',...
xc,yc,'r')
axis([-1 25 0 1.5])
axis equal
pause(0.01)
end
Run Code Online (Sandbox Code Playgroud)
是否可以通过将动画图输出到gif等方式修改代码?
先感谢您!
是的,imwrite是否支持动画GIF。就像AVI视频一样,您可以getframe按顺序抓取帧。然后将它们传递给imwriteGIF ,但是您必须先将它们从RGB转换为256色图。像这样:
for i = 1:nFrames
% draw stuff
frame = getframe(gcf);
img = frame2im(frame);
[img,cmap] = rgb2ind(img,256);
if i == 1
imwrite(img,cmap,'animation.gif','gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(img,cmap,'animation.gif','gif','WriteMode','append','DelayTime',1);
end
end
Run Code Online (Sandbox Code Playgroud)
请查看openExample('matlab/WriteAnimatedGIFExample')和以doc imwrite获取更多信息。