【问题标题】:How to add dynamic data labels inside of .avi animation?如何在 .avi 动画中添加动态数据标签?
【发布时间】:2015-02-25 06:20:22
【问题描述】:

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

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

现在我想为每个点添加动态标签 (24)。我用“文本”做了这个 但它根本不起作用,因为在动画期间标签被完全涂抹。 动画一个一个地显示标签位置,但问题是动画不会删除所有以前的 显示 next 之前的标签位置。

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

...

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 update”和“refreshdata”,但没有帮助。

【问题讨论】:

  • 那么你有没有尝试我的建议??它对你有用吗?
  • 嗨,很抱歉我迟到了。是的,你的建议很棒。一切都按我的计划进行。如果你不介意我对我的帖子还有其他问题。
  • 那太好了!您可以将其标记为已接受以关闭线程吗?今晚晚些时候我会调查你的新问题:)
  • 如何将其标记为已接受?我应该怎么办?对不起,我是新人,我不知道这里的工作原理:)。
  • 好的,我想出了如何做到这一点。 :)

标签: matlab animation labels


【解决方案1】:

我认为您只需要使用findobj 在当前坐标区中查找文本对象并在循环继续时将其删除。我使用您的代码创建了一个用于演示目的的 gif 动画,但同样适用于创建 .avi 电影。

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

我加了

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

显示我添加内容的位置。

代码:

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

然后输出:

【讨论】:

    【解决方案2】:

    我想让平面 z=0 填充颜色而不是“网格”。所以,我使用了“fill3”订单。

    动画运行没有任何错误,但是当我尝试在 Windows 媒体播放器中播放它时没有任何反应。一切都是静止的。好像什么都没记录。

    我也使用您的代码 (*.gif) 进行了尝试。令人惊讶的是它有效。我不知道为什么,但确实如此。你知道重点在哪里吗?

    这是我的代码:

    X=[];   % x - coordinate for each of 24 points
    Y=[];   % y - coordinate    -||-
    Z=[];   % z - coordinate    -||-
    
    labels=[];  % 24 different labels
    
    % Coordinates for "fill3" 
    x_1=[];
    y_1=[];
    
    z_1=[0 0 0 0]; %
    
    
    
    writerObj=VideoWriter('my_animation.avi');
    
    open(writerObj);
    
    frames=485;
    
    mov(1:n_frames)=struct('cdata',[],'colormap',[]);
    
    f=figure(1);
    set(f,'Position',[150 80 1600 900]);
    
    set(gca, 'nextplot','replacechildren');
    
    plot_1=scatter3(X,Y,Z(1,:));    % all 24 points at time t=0;
    hold on;
    fill_3=fill3(x_1,y_1,z_1,[0.8 0.8 0.8]); % plane at z=0
    
    for k=1:frames
      lab=findobj('Type','text');
      delete(lab);
      set(plot_1, 'ZData',Z(k,:)); % "k" goes from 1 to 485 for all 24 points
      text(X,Y,Z(k,:),labels); % each point has its own label
      view(-30,50);
      mov(k)=getframe(gcf);
      writeVideo(writerObj,mov(k));
    end
    
    %Close file
    close(writerObj);
    

    【讨论】:

    • 嗯,这很奇怪,使用您的代码对我来说效果很好。你用的是什么版本的 Matlab?
    • R2013a (8.1.0.604) 64 位 (win64)
    • 嗨,我已经弄清楚我的代码不起作用的原因是什么,但我不知道为什么动画会这样。
    • 如果我只使用 fill3(x_data, y_data, z_data) 一切正常。但是当我用 ..."FaceAlpha",0.65 设置透明度时...动画中没有记录任何内容。这也适用于“surf”和“mesh”。
    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多