动画图MATLAB [英] Animated plots MATLAB

查看:88
本文介绍了动画图MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MATLAB制作动画图时遇到问题.

I have problem to produce animated plots, using MATLAB.

我想绘制我的信号y 作为我的时间x 的函数(我将其保留在两个单独的变量中),然后制作它的动画,并观察其变化根据时间显示我的信号.

I want to plot my signal y as a function of my time x (that i keep in two separate variables) and then produce an animation of it, seeing variations of my signal according to time.

最后,我要生成一系列".tif"图像(用于在imageJ中读取)和".avi"电影文件.

At the end, I would like to produce both a succession of ".tif" images (for reading it in imageJ) and a ".avi" movie file.

如果有人可以向我展示方法,那将对我有很大帮助,因为我尝试自己使用MATLAB帮助和论坛来做到这一点,但我每次都失败了.

It will really help me if someone can show me the way, because i try to do it by myself using MATLAB Help and forums, but i failed every time.

提前谢谢!

推荐答案

执行此操作的标准方法是在循环中更新绘图数据,并使用

The standard way of doing this would be to update your plot data within a loop, and use getframe or a similar function to grab the current screen and save it to a file with imwrite or VideoWriter.

写入

对于 imwrite ,重要的是,如果要要写入多帧数据(对于TIFF或GIF),您想使用'WriteMode'参数并将其设置为'append',以便您只需将新帧添加到图像即可.第一次遍历循环时,您不需要要附加,因为那样会附加到可能已经存在的现有图像上.

With imwrite it is important that if you want to write multi-frame data (for either a TIFF or GIF) you want to use the 'WriteMode' parameter and set it to 'append' so that you simply add the new frame to the image. On the first time through the loop you don't want to append since that would append to an existing image that may already exist.

获取框架

对于 getframe ,它捕获了一个屏幕截图指定图形的,返回一个包含颜色图和RGB图像的结构,为cdata.这是您要写入视频或多帧图像的东西.

As far as getframe, it grabs a screenshot of the specified figure and returns a struct containing a colormap and the RGB image as cdata. This is the thing that you want to write to either your video or your multi-frame image.

VideoWriter

要写入视频,请使用 VideoWriter 类的行为略有不同.主要步骤是:

For writing to video, you'll use the VideoWriter class which behaves a little differently. The main steps are:

  1. 创建对象

  1. Create the object

vid = VideoWriter('filename.avi');

打开对象

vid.open() % or open(vid)

使用 writeVideo

Write some data using writeVideo

vid.writeVideo(data)

关闭视频

vid.close()

然后,您可以根据需要多次调用writeVideo,每次添加一个额外的帧.

Then you can call writeVideo as many times as you want and each time it will add an additional frame.

摘要

这是一个演示,它将所有内容整合在一起,并编写了多帧TIFF和AVI.

Here is a demo which brings all of that together and writes a multi-frame TIFF as well as an AVI.

% Example data to plot
x = linspace(0, 2*pi, 100);
y = sin(x);


% Set up the graphics objects
fig = figure('Color', 'w');
hax = axes();
p = plot(NaN, NaN, 'r', 'Parent', hax, 'LineWidth', 2);
set(hax, 'xlim', [0, 2*pi], 'ylim', [-1 1], 'FontSize', 15, 'LineWidth', 2);

% Create a video object
vid = VideoWriter('video.avi')
vid.open()

% Place to store the tiff
tifname = 'image.tif';

for k = 1:numel(x)
    set(p, 'XData', x(1:k), 'YData', y(1:k));

    % Grab the current screen
    imdata = getframe(fig);

    % Save the screen grab to a multi-frame tiff (using append!)
    if k > 1
        imwrite(imdata.cdata, tifname, 'WriteMode', 'append')
    else
        imwrite(imdata.cdata, tifname);
    end

    % Also write to an AVI
    vid.writeVideo(imdata.cdata);
end

% Close the video
vid.close()

结果(作为GIF动画)

这篇关于动画图MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆