【问题标题】:Creating movie by sequence of plots in matlab在matlab中按情节序列创建电影
【发布时间】:2017-07-16 02:03:48
【问题描述】:

我使用答案中的代码创建了一个动画 presenting motion of random walkers in matlab

运行答案代码后,您可以按情节顺序观看电影。有没有办法将情节序列保存为电影?

【问题讨论】:

    标签: matlab matlab-figure matlab-guide movieclip


    【解决方案1】:

    您可以通过以下步骤使用VideoWriter 对象创建情节视频:

    1) 创建并打开视频对象(同时指定视频的名称)

     vidObj = VideoWriter('SIN_X_COS_X.avi');
    

    2) 在绘图循环中,使用getframe 函数调用plot 后获取当前帧

    currFrame = getframe;
    

    3) 将当前帧写入视频文件

    writeVideo(vidObj,currFrame);
    

    4) 在绘图循环结束时关闭视频对象

    关闭(vidObj);

    关于您所指的答案的代码,您只需在步骤说明中提到的位置添加上述语句即可。

    在下文中,您可以找到建议方法的一种可能实现方式。

    % Generate some data
    t=0:.01:2*pi;
    sin_x=sin(t);
    cos_x=cos(t);
    % Open a figure and crate the axes
    figure
    axes;
    %
    % STEP 1:
    %
    % Create and open the video object
    vidObj = VideoWriter('SIN_X_COS_X.avi');
    open(vidObj);
    %
    % Loop over the data to create the video
    for i=1:length(t)
       % Plot the data
       h(1)=plot(t(i),sin_x(i),'o','markerfacecolor','r','markersize',5);
       hold on
       plot(t(1:i),sin_x(1:i),'r')
       plot(t(1:i),cos_x(1:i),'b')
       h(2)=plot(t(i),cos_x(i),'o','markerfacecolor','b','markersize',5);
       set(gca,'xlim',[0 2*pi],'ylim',[-1.3 1.3])
       %
       % STEP 2
       %
       % Get the current frame
       currFrame = getframe;
       %
       % STEP 3
       %
       % Write the current frame
       writeVideo(vidObj,currFrame);
       %
       delete(h)
    end
    %
    % STEP 4
    %
    % Close (and save) the video object
    close(vidObj);
    

    希望这会有所帮助,

    卡普拉'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多