【问题标题】:Animation of figure with subplots using VideoWriter in MATLAB在 MATLAB 中使用 VideoWriter 绘制带有子图的图形动画
【发布时间】:2018-01-26 09:13:03
【问题描述】:

我正在尝试使用VideoWriter 从具有 2 个子图的图形在 MATLAB 中创建动画文件。但是,我得到的 avi 文件只包含其中一个子图。

代码如下:

clc    
clear 
vidObj = VideoWriter('randdata');   
open(vidObj)    
figure (1)   
for i = 1:100       
    clf        
    subplot(1,2,1)        
    imagesc(rand(100))        
    subplot(1,2,2)        
    imagesc(rand(100))       
    drawnow       
    CF = getframe;        
    writeVideo(vidObj,CF);        
end

这里一定有一些简单的问题,但我不知道是什么。我想捕捉整个人物。

【问题讨论】:

  • 如果你做一个gif动画会更容易;)

标签: matlab animation matlab-figure subplot


【解决方案1】:

第一行中的documentation for getframe 状态:

F = getframe 捕获当前的

所以它捕获的是坐标轴,而不是图形。

您想按照文档中的说明使用它

F = getframe(fig) 捕获由 fig 标识的图形。 如果要捕获图窗窗口的整个内部,请指定图窗,包括轴标题、标签和刻度线。捕获的影片帧不包括图形菜单和工具栏。

所以你的代码应该是

clc; clear
vidObj = VideoWriter('randdata');
open(vidObj);
figure(1);
for ii = 1:100
    clf
    subplot(1,2,1)
    imagesc(rand(100))
    subplot(1,2,2)
    imagesc(rand(100))
    drawnow;
    % The gcf is key. You could have also used 'f = figure' earlier, and getframe(f)
    CF = getframe(gcf); 
    writeVideo(vidObj,CF);
end

为方便起见,您可能只想获得像流行(且简单)gif 这样的文件交换功能来创建动画 gif。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多