在Matlab中半径不断变化的球的gif

问题描述 投票:0回答:1

我希望根据与半径和时间有关的指定函数生成一个随时间变化尺寸(半径)的球体的.gif文件。我在制作动画方面有些麻烦。

这是我到目前为止的内容:

%% Parameters 
dt = 0.05;
time = 0:dt:1;
radius = 1 
%% generate sphere
[X, Y, Z] = sphere(25);
X=X*radius;
Y=Y*radius;
Z=Z*radius;
mySphere = surf(X,Y,Z, 'FaceLighting','gouraud');
axis equal
shading interp
mySphere.FaceAlpha = 0.3
view([61 15])
colormap bone
hold on
%% generate gif 
filename = 'Sizechange.gif'; 
for n = 1:20 

    radius = 1 + time(n)
    im = frame2im(getframe(1));
    [imind,cm] = rgb2ind(im,256);

    if n == 1;
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',dt);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',dt);
    end
end 

[这里,我试图以0.05的步长将其从半径1扩展到半径2。但是,当我运行它时,gif仍然保持为1,并且没有动画。

任何帮助将不胜感激。

matlab matlab-figure
1个回答
0
投票

正如@ cris-luengo所说,您应该为半径上的每次迭代重新绘制球体。

%% Parameters
dt = 0.05;
time = 0:dt:1;
radius = 1 ;
%% generate sphere
[X, Y, Z] = sphere(25);
X=X*radius;
Y=Y*radius;
Z=Z*radius;
%figure;
%mySphere = surf(X,Y,Z, 'FaceLighting','gouraud');
% axis equal
% shading interp
% mySphere.FaceAlpha = 0.3;
% view([61 15])
% colormap bone
% hold on
%% generate gif
filename = 'Sizechange.gif';
figure;
for n = 1:20

    radius = 1+ time(n);
    %====================================================
    X=X*radius;
    Y=Y*radius;
    Z=Z*radius;
    mySphere = surf(X,Y,Z, 'FaceLighting','gouraud');
    axis equal
    shading interp
    mySphere.FaceAlpha = 0.3;
    view([61 15])
    colormap bone
    %====================================================
    im = frame2im(getframe(1));
    [imind,cm] = rgb2ind(im,256);

    if n == 1
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',dt);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',dt);
    end
end
© www.soinside.com 2019 - 2024. All rights reserved.