【问题标题】:How to change a still plot to a movie in Matlab?如何在 Matlab 中将静止情节更改为电影?
【发布时间】:2019-02-07 04:16:34
【问题描述】:

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

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);

任何建议都会很棒。我从其他人的帖子中查看了几个不同的示例,但是他们似乎都没有以相同的方式访问电影。

【问题讨论】:

  • 我希望它能够一次铺设一层
  • 你需要使用movie吗?为什么不对每个跟踪使用plothold on,在每个跟踪的末尾使用pause
  • @rayryeng 最终产品必须是电影格式...我可以使用 plot hold on 和 hold off 然后将每个跟踪保存到文件夹中播放吗?
  • 不,但您可以使用getframe 捕获每个情节。虽然效率很低,但我可以提供答案。
  • @rayryeng 一个例子会很棒!

标签: matlab plot movie


【解决方案1】:

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 命令,即可随意播放电影。第二个参数指定您希望影片重复多少次,第三个参数指定影片每秒的帧数。

【讨论】:

  • 非常感谢@rayryeng 的解释。我会试一试的!
  • 您是指电影文件吗?你没有说清楚。你问的问题和你现在问的不是一回事。请编辑您的问题并澄清。
  • 对不起,问题是正确的,解决方案有效!输入最后一条评论时我在想自己请忽略!
  • 当然不用担心。需要帮助请叫我。否则,当您准备好时,请接受答案,让社区中的每个人都知道您不再需要帮助。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多