# matlab绘制随时间变化的曲线动画 **Repository Path**: sunyu-zju/matlab-animation ## Basic Information - **Project Name**: matlab绘制随时间变化的曲线动画 - **Description**: matlab绘制随时间变化的曲线动画 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-05-23 - **Last Updated**: 2024-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README > 最近师兄建议我在放论文实验视频的时候可以同时播放对应数据随时间变化的动画视频。查阅全网,发现好像还没有人对如何用matlab做这件事情有完整的总结。这里,我简单地总结一下我的经验。 ## 1. 准备工作 - MATLAB (笔者使用的版本为R2021a) - 数据,包括时间t和数据值。本文将数据从表格中导入 - 为了美观,对线型,配色等进行基本设置 ## 2. 读取数据 首先要对数据进行读取,这里比较重要的是需要计算画图中每个时间步之间的平均步长是多少,后面需要使用。 ```matlab % 读取表格数据 data = readtable('data.csv','VariableNamingRule','preserve'); % 计算每个时间步的平均间隔 step = data.time(size(data.time,1))/size(data.time,1); ``` ## 3. 设置视频对象 为了将生成的结果输出为一个视频,我们创建一个VideoWriter类。需要注意的是,每次画图时增加一个点,对应的视频会走一帧。为了让最后生成的视频长度和你画图的时间相对应,我们设定视频的帧率为每个时间步走一帧。 ```matlab %创建视频类对象,命名并设置其格式 video = VideoWriter('animation.mp4', 'MPEG-4'); %设置视频帧率 video.FrameRate = 1/step; open(video); ``` ## 4. 设置基本线的格式 接下来设定曲线的形式,colorList中是我们选择的配色 ```matlab hLine_1 = animatedline('Color',colorList(1,:),'LineWidth',5); hLine_2 = animatedline('Color',colorList(2,:),'LineWidth',5); hLine_3 = animatedline('Color',colorList(3,:),'LineWidth',5); ``` ## 5. 将绘图操作更新到动画中 接下来,使用addpoints()函数,每一步都向曲线中添加数据中的一个点。 每添加一个点,使用getframe()函数记录当前图像,再添加到视频中。 ```matlab for i = 1:size(data.time,1) % 将绘图操作添加到动画中 addpoints(hLine_1, data{i, 'time'}, data{i, 'data.x'}); addpoints(hLine_2, data{i, 'time'}, data{i, 'data.y'}); addpoints(hLine_3, data{i, 'time'}, data{i, 'data.z'}); drawnow; % 刷新图形 frame = getframe(gcf); writeVideo(video, frame); end close(video); ``` ## 6. 补充一下我的画图设置,画出来的图片更美观 ```matlab %对画图进行基本设置 colorList=[ 084 134 135;... 071 051 053;... 189 030 030]./255; figure('Position',[500,200,600,580],'Name','data') ax=gca;hold on hLine_1 = animatedline('Color',colorList(1,:),'LineWidth',5); hLine_2 = animatedline('Color',colorList(2,:),'LineWidth',5); hLine_3 = animatedline('Color',colorList(3,:),'LineWidth',5); patchHdl(1)=fill([0,0,0,0],[0,0,0,0],colorList(1,:),'LineWidth',1.2); patchHdl(2)=fill([0,0,0,0],[0,0,0,0],colorList(2,:),'LineWidth',1.2); patchHdl(3)=fill([0,0,0,0],[0,0,0,0],colorList(3,:),'LineWidth',1.2); h = legend(patchHdl,{'$x$','$y$','$z$'},'AutoUpdate','off',... 'Location','northoutside','NumColumns',4,'Box','off','FontSize',18); set(h,'Interpreter','latex','FontName','Times New Roman','FontSize',18,'FontWeight','bold'); % 调整边缘空间 ax.LooseInset=[0,0,0,0]; % 控制轴范围 ax.YLim=[-0.1,0.1]; ax.XLim=[0,15]; ax.XTick=0:5:15; ax.YTick=-0.1:0.05:0.1; % 修改xy轴标签和字体 ax.XLabel.String='Time(s)'; ax.YLabel.String='Data'; ax.XLabel.FontSize=15; ax.YLabel.FontSize=15; ax.XLabel.FontName='Times New Roman'; ax.YLabel.FontName='Times New Roman'; ax.XLabel.FontWeight='bold'; ax.YLabel.FontWeight='bold'; ax.FontSize = 18; ax.LineWidth=1.5; ax.TickDir='out'; xLineHdl=plot([2.025,2.025],[-10,10],'LineWidth',1.8,'LineStyle','--','Color',[0,0,0]); uistack(xLineHdl,'bottom') xLineHd2=plot([7.12,7.12],[-10,10],'LineWidth',1.8,'LineStyle','--','Color',[0,0,0]); uistack(xLineHd2,'bottom') xLineHd3=plot([8.12,8.12],[-10,10],'LineWidth',1.8,'LineStyle','--','Color',[0,0,0]); uistack(xLineHd3,'bottom') xLineHd4=plot([12.2,12.2],[-10,10],'LineWidth',1.8,'LineStyle','--','Color',[0,0,0]); uistack(xLineHd4,'bottom') text(2.075, 0.09, '①','FontSize',18,'FontWeight','bold'); text(7.15, 0.09, '②','FontSize',18,'FontWeight','bold'); text(8.15, 0.09, '③','FontSize',18,'FontWeight','bold'); text(12.25, 0.09, '④','FontSize',18,'FontWeight','bold'); grid on; ``` emmm, 画起来还是很好看的嘛~ 为了方便大家参考,我将本文中用的代码和数据上传到gitee上,链接:[matlab绘制随时间变化的曲线动画](https://gitee.com/sunyu-zju/matlab-animation) ![anim](anim.gif)