首页/文章/ 详情

过冷水带你用matlab制作演示动画

3年前浏览4239

image.png

        随着这信息技术的发展,“南抖音北快手”走进大家的生活,更多的人喜欢上看视频,这种能动的图像,科研学习领域有时候为了能够很好的把事情说清楚也需要能够动态展示计算结果,过冷水本期就和大家一起学习MATLAB制作动态动画,并且保存。

           动画的原理就是多张静态连贯图像在短时间内快速播放而形成的影片。因此我们需要具备三个条件:静态图像、图像连贯、快速展示。使用绘图命令可以获得静态图像,自变量取不同的值可以保证图像空间上的连贯性,使用循环可以达到快速播放效果。完美!

    具体实现过程如下:定义自变量的取值情况,根据自变量初始化因变量。定义一个for循环构架,取k为循环计次,每次绘制各变量1至k大小的图像。由于matlab绘制速度极快,肉眼几乎分辨不处重画造成的画面顿挫感。每绘制完成一次获取当前图像帧,使用imwrite命令来保存当前帧,并使用gif图像格式,因为保存视频格式既浪费存储空间又没得必要。

lxx.gif

clear
tic
height=11.1087999344000004;width=11.1087999344000004;long=11.1087999344000004;
N=52;
load('data.mat')
for i=1:length(a)/N
   b= a(1 (N*(i-1)):N*i,:);
   Centers{i,1}=b;
end
gg = 1;
figure1 =figure('Position',[395 86 894  700],'Name','Mg原子三维空间运动轨迹','NumberTitle','off','Color','w','Menubar','figure','WindowState','maximized');
axes1 = axes('Parent',figure1);
view(axes1,[-37.5 30]);
for k=1:length(Centers)
    centers=Centers{k,1};
    fill3([0 14 14 0],[0 0 14 14],[0 0 0 0],[0 1 1],'Parent',axes1);
    hold(axes1,'on');
    fill3([0 14 14 0],[14 14 14 14],[0 0 14 14],[0 0 1],'Parent',axes1);
    fill3([14 14 14 14],[0 0 14 14],[0 14 14 0],[1 0 0],'Parent',axes1);
    plot3(centers(:,1),centers(:,2),centers(:,3),'Parent',axes1,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 0.447058826684952 0.74117648601532],'MarkerSize',8,'Marker','o','LineStyle','none');
    hold(axes1,'off');
    str1=['01';'02';'03';'04';'05';'06';'07';'08';'09';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';'21';'22';'23';'24';'25';'26';'27';'28';'29';'30';'31';'32';'33';'34';'35';'36';'37';'38';'39';'40';'41';'42';'43';'44';'45';'46';'47';'48';'49';'50';'51';'52'];
    text(axes1,centers(:,1),centers(:,2),centers(:,3),str1,'FontSize',14,'fontname','楷体','Color','red');
    fmat(:,k)=getframe;           %拷贝祯到矩阵fmat中
    im = frame2im(getframe);
    [I,map] = rgb2ind(im,256);
    if gg == 1
        imwrite(I,map,'lxx.gif','GIF', 'Loopcount',inf,'DelayTime',0.05);
        gg = gg   1;
    else
        imwrite(I,map,'lxx.gif','WriteMode','append','DelayTime',0.05);
    end
end
% 主视图XZ
figure1 = figure('Name','Mg原子三维空间运动轨迹-XZ主视图','NumberTitle','off','Color','w','Menubar','figure','WindowState','maximized');
axes1 = axes('Parent',figure1);
for k=1:length(Centers)
    centers=Centers{k,1};
    plot(centers(:,1),centers(:,3),'Parent',axes1,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',8,'Marker','o','LineStyle','none');
    str1=['01';'02';'03';'04';'05';'06';'07';'08';'09';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';'21';'22';'23';'24';'25';'26';'27';'28';'29';'30';'31';'32';'33';'34';'35';'36';'37';'38';'39';'40';'41';'42';'43';'44';'45';'46';'47';'48';'49';'50';'51';'52'];
    text(axes1,centers(:,1),centers(:,3),str1,'FontSize',14,'fontname','楷体','Color','red');
    xlim(axes1,[-1 14]);
    ylim(axes1,[-1 14]);
    box(axes1,'on');
    ylabel(axes1,'$Z$','FontSize',20,'Interpreter','latex');
    xlabel(axes1,'$X$','FontSize',20,'Interpreter','latex');
    title('XZ主视图','FontSize',20,'FontName','楷体');
    fmat(:,k)=getframe;           %拷贝祯到矩阵fmat中
end

% 左视图YZ
figure1 = figure('Name','Mg原子三维空间运动轨迹-YZ主视图','NumberTitle','off','Color','w','Menubar','figure','WindowState','maximized');
axes1 = axes('Parent',figure1);
%hold(axes1,'on');
for k=1:length(Centers)
    centers=Centers{k,1};
    plot(centers(:,2),centers(:,3),'Parent',axes1,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',8,'Marker','o','LineStyle','none');
    str1=['01';'02';'03';'04';'05';'06';'07';'08';'09';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';'21';'22';'23';'24';'25';'26';'27';'28';'29';'30';'31';'32';'33';'34';'35';'36';'37';'38';'39';'40';'41';'42';'43';'44';'45';'46';'47';'48';'49';'50';'51';'52'];
    text(axes1,centers(:,1),centers(:,3),str1,'FontSize',14,'fontname','楷体','Color','red');
    xlim(axes1,[-1 14]);
    ylim(axes1,[-1 14]);
    box(axes1,'on');
    ylabel(axes1,'$Z$','FontSize',20,'Interpreter','latex');
    xlabel(axes1,'$Y$','FontSize',20,'Interpreter','latex');
    title('YZ左视图','FontSize',20,'FontName','楷体');
    fmat(:,k)=getframe;           %拷贝祯到矩阵fmat中
end

% 俯视图 XY
figure1 = figure('Name','Mg原子三维空间运动轨迹-YZ主视图','NumberTitle','off','Color','w','Menubar','figure','WindowState','maximized');
axes1 = axes('Parent',figure1);
gg == 1
for k=1:length(Centers)
    centers=Centers{k,1};
    plot(centers(:,1),centers(:,2),'Parent',axes1,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',8,'Marker','o','LineStyle','none');
    str1=['01';'02';'03';'04';'05';'06';'07';'08';'09';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';'21';'22';'23';'24';'25';'26';'27';'28';'29';'30';'31';'32';'33';'34';'35';'36';'37';'38';'39';'40';'41';'42';'43';'44';'45';'46';'47';'48';'49';'50';'51';'52'];
    text(axes1,centers(:,1),centers(:,2),str1,'FontSize',14,'fontname','楷体','Color','red');
    xlim(axes1,[-1 14]);
    ylim(axes1,[-1 14]);
    box(axes1,'on');
    ylabel(axes1,'$Y$','FontSize',20,'Interpreter','latex');
    xlabel(axes1,'$X$','FontSize',20,'Interpreter','latex');
    title('XY俯视图','FontSize',20,'FontName','楷体');
    fmat(:,k)=getframe;           %拷贝祯到矩阵fmat中
   im = frame2im(getframe);
    [I,map] = rgb2ind(im,256);
    if gg == 1
        imwrite(I,map,'lxx1.gif','GIF', 'Loopcount',inf,'DelayTime',0.05);
        gg = gg   1;
    else
        imwrite(I,map,'lxx1.gif','WriteMode','append','DelayTime',0.05);
    end
end

也可以绘制一个粒子在三维上的运动轨迹,这样看起来会有规律一点。

lxx2.gif

%原子轨迹追踪图
figure1 = figure('Name','Mg原子三维空间运动轨迹-YZ主视图','NumberTitle','off','Color','w','Menubar','figure','WindowState','maximized');
subplot1 = subplot(2,2,1,'Parent',figure1);
view(subplot1,[-37.5 30]);
subplot2 = subplot(2,2,2,'Parent',figure1);
subplot3 = subplot(2,2,3,'Parent',figure1);
subplot4 = subplot(2,2,4,'Parent',figure1);
hold(subplot1,'on');
hold(subplot2,'on');
hold(subplot3,'on');
hold(subplot4,'on');
fill3([0 14 14 0],[0 0 14 14],[0 0 0 0],[0 1 1],'Parent',subplot1);
fill3([0 14 14 0],[14 14 14 14],[0 0 14 14],[0 0 1],'Parent',subplot1);
fill3([14 14 14 14],[0 0 14 14],[0 14 14 0],[1 0 0],'Parent',subplot1);
xlim(subplot1,[-1 14]);
ylim(subplot1,[-1 14]);
zlim(subplot1,[-1 14]); 
ylabel(subplot1,'$Y$','FontSize',20,'Interpreter','latex');
xlabel(subplot1,'$X$','FontSize',20,'Interpreter','latex');
zlabel(subplot1,'$Z$','FontSize',20,'Interpreter','latex');
title(subplot1,'三维','FontSize',20,'FontName','楷体');

xlim(subplot2,[1 length(Centers)]);
ylim(subplot2,[-1 14]);
ylabel(subplot2,'$X$','FontSize',20,'Interpreter','latex');
xlabel(subplot2,'$T$','FontSize',20,'Interpreter','latex');
title(subplot2,'TX位移','FontSize',20,'FontName','楷体');

xlim(subplot3,[1 length(Centers)]);
ylim(subplot3,[-1 14]);
ylabel(subplot3,'$Y$','FontSize',20,'Interpreter','latex');
xlabel(subplot3,'$T$','FontSize',20,'Interpreter','latex');
title(subplot3,'TY位移','FontSize',20,'FontName','楷体');

xlim(subplot4,[1 length(Centers)]);
ylim(subplot4,[-1 14]);
ylabel(subplot4,'$Z$','FontSize',20,'Interpreter','latex');
xlabel(subplot4,'$T$','FontSize',20,'Interpreter','latex');
title(subplot4,'TZ位移','FontSize',20,'FontName','楷体');
gg == 1;
for k=1:length(Centers)
   centers=Centers{k,1};
   plot3(centers(1,1),centers(1,2),centers(1,3),'Parent',subplot1,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',4,'Marker','o','LineStyle','none');
   str1=['01'];
   text(subplot1,centers(1,1),centers(1,2),centers(1,3),str1,'FontSize',14,'fontname','楷体','Color','red');

   plot(k,centers(1,1),'Parent',subplot2,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',4,'Marker','o','LineStyle','none');
   str1=['01'];
   text(subplot2,k,centers(1,1),str1,'FontSize',14,'fontname','楷体','Color','red');

   plot(k,centers(1,2),'Parent',subplot3,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',4,'Marker','o','LineStyle','none');
   str1=['01'];
   text(subplot3,k,centers(1,2),str1,'FontSize',14,'fontname','楷体','Color','red');

   plot(k,centers(1,3),'Parent',subplot4,'MarkerFaceColor',[0 1 1],'MarkerEdgeColor',[0 1 1],'MarkerSize',4,'Marker','o','LineStyle','none');
   str1=['01'];
   text(subplot4,k,centers(1,3),str1,'FontSize',14,'fontname','楷体','Color','red');
   fmat(:,k)=getframe;           %拷贝祯到矩阵fmat中
   im = frame2im(getframe);
   [I,map] = rgb2ind(im,256);
    if gg == 1
        imwrite(I,map,'lxx2.gif','GIF', 'Loopcount',inf,'DelayTime',0.05);
    else
        imwrite(I,map,'lxx2.gif','WriteMode','append','DelayTime',0.05);
    end
end

        该案例是过冷水在学习动力学的时候随手研究的新技术,当时想的是能不能实现动态绘图,尝试一下,于是就做了该图,等把图画出来后感觉好有规律,立刻就理解了该怎么用动力学坐标数据研究扩散系数。包括怎么统计其计算规律,绘制电子云图,这就是非常典型的正反馈,越做越懂,越懂越做。本期过冷水就简单的给大家分享这么多,欢迎大家留言讨论,提供好的idea,过冷水帮你实现。

        原创文章,作者过冷水,未经授权,禁止私自转载,转载请联系作者,如果您希望加入Matlab仿真秀官方交流群和资料下载可加群:927550334

精品回顾>>>>>>

matlab绘制农夫过河动态图

用matlab实现复杂累计求和的展开(程序分享)

Matlab 做重要性抽样 含代码

MATLAB/ADAMS求解动力学

Simulink电力电子技术建模和仿真

微信图片_20210415161543.jpg

附件

20积分data.rar
理论科普代码&命令MATLAB
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2021-04-16
最近编辑:3年前
过冷水
博士 | 讲师 讨论号:927550334
获赞 355粉丝 175文章 109课程 11
点赞
收藏

作者推荐

未登录
1条评论
过冷水
讨论号:927550334
3年前
仿真平台的matlab仿真交流群正式创建。**:927550334。欢迎加入Matlab仿真圈旨在为大家提供一个良好的matlab学习交流平台。群内长期分享各类与matlab编程有关的资料程序,群内有各行各业精通matlab的资深从业者算法工匠 好学懿 伟sss坐镇指导,能够解决matlab应用过程中的各种难题。欢迎大家入圈。
回复

课程
培训
服务
行家

VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈