代码如下
clc;clear;close all;
fig=figure;
axis([0,2*pi,-1,1]);
x=linspace(0,2*pi,20);
y=sin(x);
h=animatedline("Color",'r',"Marker","o","MarkerSize",10,"LineWidth",3);
im=cell(1,length(x));
for i = 1:length(x)
addpoints(h,x(i),y(i));
drawnow
frame = getframe(fig);
im{i} = frame2im(frame);
end
filename = 'C:\Users\13349\Desktop\BILIBILI稿件\MATLAB动图\MATLAB1_3.gif'; % Specify the output file name
for idx = 1:length(x)
[A,map] = rgb2ind(im{idx},256);
if idx == 1
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1);
end
end