matlab 使用 contourf 或 scatter 绘制动图输出成视频,避免帧与帧之间重合

一般的读取场的每个时刻的状态,然后输出成视频的 matlab 代码类似这样

video_name = 'zeta_video';
obj = VideoWriter(video_name);
obj.FrameRate = 10;
open(obj);

T_STEP = 1.8e-3;
MAX_STEP = 50000;
OUTPUT_GAP = 500;

x = linspace(0, 8, 513);
y = linspace(0, 8, 513);
[X,Y] = meshgrid(x,y);

levels = linspace(-15, 15, 100);

t_len = MAX_STEP/OUTPUT_GAP;

n = 0:OUTPUT_GAP:MAX_STEP-OUTPUT_GAP;

nImages = length(n);
fig = figure;
%Draw multiple images to display and store each image into a cell array%
for idx = 1:nImages
    zeta = csvread(sprintf('./zeta/zeta_%d.csv', n(idx)));
    [C,h] = contourf(X, Y, zeta', levels);
    clim([-15 15]);
    xlim([1 3]);
    ylim([3 5]);
    colorbar;
    set(h,'LineColor','none');
    title(sprintf('zeta t = %.2fs', n(idx)*T_STEP));
    drawnow
    frame = getframe(fig); 
    writeVideo(obj,frame);
end

close(obj);

后来我想用 scatter 画的时候,发现帧与帧之间的图像重合了

video_name = 'p_video';
obj = VideoWriter(video_name);
obj.FrameRate = 20;
open(obj);

T_STEP = 1.8e-3;
MAX_STEP = 50000;
OUTPUT_GAP = 200;

x = linspace(0, 8, 512);
y = linspace(0, 8, 512);
[X,Y] = meshgrid(x,y);

levels = linspace(-0.001, 0.001, 100);

t_len = MAX_STEP/OUTPUT_GAP;

n = OUTPUT_GAP:OUTPUT_GAP:MAX_STEP-OUTPUT_GAP;

nImages = length(n);
fig = figure;
%Draw multiple images to display and store each image into a cell array%
for idx = 1:nImages
    p = csvread(sprintf('./p/p_%d.csv', n(idx)));
    p = p';

    ax = axes();


    scatter(X(:), Y(:), [], p(:), 'filled');

    colormap(ax, jet(numel(levels)-1)); % set colormap
    caxis(levels([1,end]));             % set color range
    cb = colorbar();
    
    xlim([1 3]);
    ylim([3 5]);

    title(sprintf('zeta t = %.2fs', n(idx)*T_STEP));
    drawnow
    frame = getframe(fig); 
    writeVideo(obj,frame);
end\

close(obj);

最后发现是我在循环里面创建 axes 的缘故

改成

ax = axes();

%Draw multiple images to display and store each image into a cell array%
for idx = 1:nImages
    ...
end

就好了

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值