如何在 Matlab 中将静止图更改为电影?

How to change a still plot to a movie in Matlab?

我想了解 Movie in Matlab 是如何工作的,我以前从未使用过它。我有一个热方程式的代码,可以找到我决定放在物体(杆)长度上的段数的温度。但是,我能够绘制一个正常图,显示不同条件下的所有值。我在左场试图了解如何将它变成电影。

A=0.1; %Diffusivity or conductivity (switching for variable K)
L=1; %Length of rod
dt= 10^(-4); %value of timestep
X=100; %Wants to Discretize rod into 100 separate sections
t= 10; % 50 timesteps will be taken (dt)
dx=L/X; %delta X needed for the heat equation for this case Pi/100
x1=linspace(0, L, X); %Take 100 equally spaced sections between 0 and L
y1=linspace(0, t, 51);
T1= zeros(X,t); %set a X x t matrix of zeros
%Set boundary and initial conditions
%T0=0; Ends of rod temperature are 0
%TL=0;
%Create a for loop to continuously solve the heat equation until time is up
    for i= 2:X-1 %X-1 because we already know initial point
        for j=1:50 % used to update heat equation each step
            T1(:,1)= sin((pi*x1(i))/L);%start solving the heat equation at second
                                  % value in the matrix in column 1 (skip
                                  % 1st because we have initial condition)
            T1(1,:) = 0; %Gives the first column = 0
            T1(100,:) = 0; %Gives last column = 0  just as initial conditions state
            %Heat Equation for T^n+1 (i)
            T1(i,j+1)= A*dt*((T1(i+1,j)-2*T1(i,j)+T1(i-1,j)/dx^2))+ T1(i,j);

        end
    end
figure(1)
plot(x1,T1);

任何建议都很好。我看过几个来自其他人 post 的不同例子,但他们似乎都不会以相同的方式访问电影。

movie 要求你在内存中有一系列的 RGB 图像,并将它们存储在为 movie 函数设计的结构数组中。

您可以使用 hold on 一次绘制一个信号。此外,您可以使用 getframe 将每个情节实际捕获到图像中以获得 movie 所需的格式,将其全部连接成一个结构数组并最终播放电影。

我看到您正在同时绘制所有信号,每个信号一列。为简化此操作,制作另一个 for 循环并分别循环遍历每个信号 - 每列一个并一次绘制这些信号。

A=0.1; %Diffusivity or conductivity (switching for variable K)
L=1; %Length of rod
dt= 10^(-4); %value of timestep
X=100; %Wants to Discretize rod into 100 separate sections
t= 10; % 50 timesteps will be taken (dt)
dx=L/X; %delta X needed for the heat equation for this case Pi/100
x1=linspace(0, L, X); %Take 100 equally spaced sections between 0 and L
y1=linspace(0, t, 51);
T1= zeros(X,t); %set a X x t matrix of zeros
%Set boundary and initial conditions
%T0=0; Ends of rod temperature are 0
%TL=0;
%Create a for loop to continuously solve the heat equation until time is up
for i= 2:X-1 %X-1 because we already know initial point
    for j=1:50 % used to update heat equation each step
        T1(:,1)= sin((pi*x1(i))/L);%start solving the heat equation at second
                                  % value in the matrix in column 1 (skip
                                  % 1st because we have initial condition)
        T1(1,:) = 0; %Gives the first column = 0
        T1(100,:) = 0; %Gives last column = 0  just as initial conditions state
        %Heat Equation for T^n+1 (i)
        T1(i,j+1)= A*dt*((T1(i+1,j)-2*T1(i,j)+T1(i-1,j)/dx^2))+ T1(i,j);    
    end
end
figure(1)
hold on; % New

% New - To store the frames for the movie
frames = repmat(struct('cdata', [], 'colormap', []), 50, 1);
for j = 2 : 51
    plot(x1,T1(:,j));
    frames(j - 1) = getframe(gcf);        
end
close all; % Close the figure
% Play the movie
figure;
movie(frames, 1, 5); % Play the movie once at 5 frames per second

新代码会将所需的绘图存储为图像 - 您必须查看图形绘图才能抓取帧。之后,我们关闭图形并以 5 FPS 播放电影。在这一点之后,您只需 运行 最后一个 movie 命令就可以随意播放电影。第二个参数指定您希望影片重复多少次,第三个参数指定影片每秒的帧数。