且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matlab中的时间序列动画

更新时间:2023-02-26 15:46:06

在Matlab中,您可以使用 getFrame writeVideo 函数.对于一个非常普通的案例,我会加以解释,然后将其应用于您的案例.

假设我们有一个图,该图使用示例函数 solverIteration (组成...)在for循环内的每次迭代(更改PDE等时经常发生)中更改其数据.我们正在域 x 上绘制矢量 y .

要录制视频,我们必须执行以下操作:

  video = VideoWriter('myVideo.avi');%创建一个视频对象打开视频);%打开视频源-限制在程序中使用视频对于m = 1:Ky = SolverIteration(y);情节(x,y);拉多vidFrame = getframe(gcf);%而不是gcf,您可以指定要捕获的图形clf;writeVideo(video,vidFrame);%将帧添加到视频结尾关闭(视频); 

此脚本是如何录制视频的示例.在官方 matlab网站中有一些示例和说明..>

I am new to working with time series in Matlab and am struggling with getting this going. I have time series heat-transfer data (over a period of 20ms in steps of 1 microsecond) at these 11 locations (see code below). I am clueless as to how I could put them together to be able to generate a plot at each time step and use getframe at each timestep to make an animation. Any help on how to get started with this would be much appreciated. Here is a link to the 11 data files, providing time on column1 and heat transfer on column2: https://drive.google.com/open?id=1oDAdapqvL-blecb7BOLzxpeiJBsqLd59

Please feel free to suggest any other tools (matplotlib/plotly etc.) that may be better in this scenario as well. Thanks a ton!

close all
clear all

x1=399.5
x2=400.5


y0=0 
y1=4
y2=8
y3=12
y4=16
y5=20
y6=-4
y7=-8
y8=-12
y9=-16
y10=-20

%The gauge locations for the first row will be [x1,y1], [x1,y3], [x1,y5], [x1,y6], [x1,y8],
%[x1,y10]

%The gauge locations for the second row will be [x2,y0], [x2,y2], [x2,y4], [x2,y7],
%[x2,y9]

figure

plot(x1,y1,'r.', x1,y3,'r.', x1, y5, 'r.', x1, y6, 'r.', x1, y8, 'r.', x1, y10, 'r.')
hold
plot(x2,y0,'b.', x2,y2,'b.', x2, y4, 'b.', x2, y7, 'b.', x2, y9, 'b.')
axis([390 410 -30 30])

In Matlab you can use, as you said the getFrame and writeVideo functions. Ill explain it for a very general case, which you can then apply to yours.

Let's say we have a plot that changes it's data at every iteration inside a for loop (happens frequently when solving PDEs and so on) with an exemplary function solverIteration (made up...). We are plotting a vector y over our domain x.

In order to record the video we have to do the following:

video = VideoWriter('myVideo.avi'); %Create a video object
open(video); % Open video source - restricts the use of video for your program

for m=1:K
    y = solverIteration(y);
    plot(x,y);
    drawnow;

    vidFrame = getframe(gcf);
    % instead of gcf you can specify which figure you want to capture

    clf;

    writeVideo(video,vidFrame); % adds frames to the video

end


close(video);

This script is an example for how to record a video. There are several examples and explanations at the official matlab site.