如何在 MATLAB 中将 3D 动画线保存为视频或 gif
How can I save a 3D animated line as a video or gif in MATLAB
我想在 3d 中模拟粒子的轨迹,所以我使用以下代码创建了这个小型模拟:
nP=100;
N=100;
z=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
f = figure
view(3);
for i=N
h = animatedline('MaximumNumPoints', 1.e4,'color',rand(1,3));
for k = 1:length(x)
addpoints(h,x(i,k),y(i,k),z(i,k));
drawnow
end
end
hold on
hold off
numpoints = 500;
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:N
addpoints(h,x(k),y(k),z(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 或 mp4。一切正常,但最终保存的是二维粒子的轨迹。关于如何使这项工作有任何想法吗?
您的代码有误:
z=rand(nP,N);
y=rand(nP,N);
z=rand(nP,N);
应该是(见第一行):
x=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
在第二部分把view(3)
放在drawnow
之前
...
for k = 1:N
addpoints(h,x(k),y(k),z(k))
%addpoints(h2,x(k),y2(k))
view(3);
drawnow
...
完整代码,对我有用:
nP=100;
N=100;
x=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
f = figure
view(3);
for i=N
h = animatedline('MaximumNumPoints', 1.e4,'color',rand(1,3));
for k = 1:length(x)
addpoints(h,x(i,k),y(i,k),z(i,k));
drawnow
end
end
hold on
hold off
numpoints = 500;
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:N
addpoints(h,x(k),y(k),z(k))
%addpoints(h2,x(k),y2(k))
view(3);
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
更新:抱歉,我没有注意到 Luis Mendo 的评论。
我想在 3d 中模拟粒子的轨迹,所以我使用以下代码创建了这个小型模拟:
nP=100;
N=100;
z=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
f = figure
view(3);
for i=N
h = animatedline('MaximumNumPoints', 1.e4,'color',rand(1,3));
for k = 1:length(x)
addpoints(h,x(i,k),y(i,k),z(i,k));
drawnow
end
end
hold on
hold off
numpoints = 500;
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:N
addpoints(h,x(k),y(k),z(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 或 mp4。一切正常,但最终保存的是二维粒子的轨迹。关于如何使这项工作有任何想法吗?
您的代码有误:
z=rand(nP,N);
y=rand(nP,N);
z=rand(nP,N);
应该是(见第一行):
x=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
在第二部分把view(3)
放在drawnow
...
for k = 1:N
addpoints(h,x(k),y(k),z(k))
%addpoints(h2,x(k),y2(k))
view(3);
drawnow
...
完整代码,对我有用:
nP=100;
N=100;
x=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
f = figure
view(3);
for i=N
h = animatedline('MaximumNumPoints', 1.e4,'color',rand(1,3));
for k = 1:length(x)
addpoints(h,x(i,k),y(i,k),z(i,k));
drawnow
end
end
hold on
hold off
numpoints = 500;
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:N
addpoints(h,x(k),y(k),z(k))
%addpoints(h2,x(k),y2(k))
view(3);
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