0

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).

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.

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

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

2 Answers2

0

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.

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).

I added

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

to show where I added stuff.

Code:

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

And output:

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
0

Instead of "mesh" I would like to have plane z=0 filled with color. So, I've used "fill3" order.

Animation run without any error but when I tried to play it in windows media player nothing happened. Everything was static. It seems like nothing was recorded.

I also tried this with your code (*.gif). Surprisingly it works. I don't know why, but it does. Do you know where is the point?

Here is my code:

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);