如何在.avi动画中添加动态数据标签? [英] How to add dynamic data labels inside of .avi animation?

查看:75
本文介绍了如何在.avi动画中添加动态数据标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用"scatter3"制作了3D动画(.avi).我有3组数据点 X,Y(均为矩阵1到24)是固定数据点,Z为矩阵485到24 (仅Z坐标随时间变化).

I've made 3D animation (.avi) with "scatter3". I have 3 sets of data points where X,Y (both are matrix 1 by 24) are fixed data points and Z is matrix 485 by 24 (only Z coordinate changes with time).

我还包括使用网格"的网格,其中所有数据点的z均等于0. 这只是为了说明平面z = 0,因为我的数据点在-14和15之间交替.

I also include grid using "mesh", where all data points have z equal to 0. This is just to illustrate plane z=0, because my data points alternate between -14 and 15.

现在,我想为每个点添加动态标签(24).我是用文字"完成的 但它根本不起作用,因为在动画过程中标签完全被弄脏了. 动画显示标签位置一个接一个,但是问题是动画不能擦除所有先前的标签位置 显示下一个之前的标签位置.

Now I would like to add dynamic labels for each point (24). I did this with "text" but it doesn't work at all, because during the animation labels are completely blotted. Animation shows label positions one by another but problem is that animation doesn't erase all the previous label positions before next is shown.

这是我创建动画的代码部分:

Here is the section of my code where I create the animation:

...

X=[];   % x - coordinate for each of 24 points
Y=[];   % y - coordinate    -||-
Z=[];   % z - coordinate    -||-

labels=[];  % 24 different labels

a=1:1:24;
b=1:1:24;

[aa bb]=meshgrid(a,b);

c=aa*0+bb*0;



writerObj=VideoWriter('my_animation.avi');

open(writerObj);

frames=485;

mov(1:n_frame)=struct('cdata',[],'colormap',[]);

set(gca, 'nextplot','replacechildren');

f=figure(1);
set(f,'Position',[150 80 1600 900]);

plot_1=scatter3(X,Y,Z(1,:));    % all 24 points at time t=0;
hold on;
net=mesh(a,b,c,'EdgeColor',[0 0 0],'FaceColor','none'); % grid at z=0

for k=1:frames
   set(plot_1, 'ZData',Z(k,:)); % "k" goes from 1 to 485 for all 24 points
   set(net,    'ZData');        % mesh is static all the time
   text(X,Y,Z(k,:),labels); % each point has its own label
   view(-30,50);
   mov(k)=getframe(gcf);
   writeVideo(writerObj,mov(k));
end

有什么主意我该如何解决?我尝试在for循环中使用"drawnow更新"和"refreshdata",但这无济于事.

Any ideas how could I fix this? I tried with "drawnow update" and "refreshdata" inside of for loop but it doesn't help.

推荐答案

我认为您只需要使用findobj在当前轴上查找文本对象,并在循环进行时将其删除.我使用您的代码创建了用于演示目的的gif动画,但是创建.avi电影也是如此.

I think you simply need to use findobj to look for text object in the current axes and delete them as your loop goes on. I used your code to create a gif animation for demo purposes but the same applies for creating an .avi movie.

这是代码;我使用了虚拟数据,并在循环中添加了对pause的调用.我还使用了虚拟标签,即当前帧(我只使用10而不是485).

Here is the code; I used dummy data and added a call to pause in the loop. I also use dummy labels, i.e. the current frame (I only use 10 instead of 485).

我添加了

%// NEW =========

显示我在哪里添加内容.

to show where I added stuff.

代码:

clc
clear

%// Create dummy data
X=1:24;   % x - coordinate for each of 24 points
Y=1:24;   % y - coordinate    -||-
Z=rand(10,24);   % z - coordinate    -||-

a=1:1:24;
b=1:1:24;

[aa, bb]=meshgrid(a,b);

c=aa*0+bb*0;

frames=10;

f=figure(1);
set(f,'Position',[150 80 1600 900]);

plot_1=scatter3(X,Y,Z(1,:));    % all 24 points at time t=0;

%// Moved after creating an axes. Otherwise useless figure created.
set(gca, 'nextplot','replacechildren');

hold on;
net=mesh(a,b,c,'EdgeColor',[0 0 0],'FaceColor','none'); % grid at z=0

filename = 'MyAnimation.gif';
for k=1:frames

    %// NEW =======================
    %// Use findobj to look for 
    hText = findobj('Type','text');
    delete(hText);
    %//============================


    %// NEW =======================
    Label = num2str(k);
    %//============================

    set(plot_1, 'ZData',Z(k,:)); % "k" goes from 1 to 485 for all 24 points

    text(X+.05,Y+.05,Z(k,:)+.05,Label); % each point has its own label
    view(-30,50);

    %// NEW =======================
    zlim([0 1]) %// Keep axis Z-limit constant

    %// NEW =======================
    frame = getframe(1);
      im = frame2im(frame);
      [imind,cm] = rgb2ind(im,256);
      if k == 1;
          imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
      else
          imwrite(imind,cm,filename,'gif','WriteMode','append');
      end
    %//============================


    %// NEW =======================
    pause(.2)
    %//============================
end

并输出:

这篇关于如何在.avi动画中添加动态数据标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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