0
votes

There is example of the web that shows how to do animated plot in a single figure.

However, I want to do two subplots in a single figure, such that they will show animation in a first subplot, and then the animation ina second subplot.

Using 'figure(1)' or 'figure (2)' and 'hold on', I can do the animation plot as follows. However, How do I call the subplot to do the similiar things?

So the effect I am looking for is: 1) figure that is opened and has two subplot. 2) plot the animated curve in the 1st subplot, then plot the animated curve in the 2nd subplot. 3) I want to go back to the 1st subplot to plot more things, and also go to 2nd subplot to plot more things.

figure(1); hold on; x = 1:1000;
y = x.^2;

%// Plot starts here
figure,hold on

%// Set x and y limits of the plot
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])

%// Plot point by point
for k = 1:numel(x)
    plot(x(k),y(k),'-') %// Choose your own marker here

    %// MATLAB pauses for 0.001 sec before moving on to execue the next 
    %%// instruction and thus creating animation effect
    pause(0.001);     
end
3

3 Answers

2
votes

Just do the subplot's in the loop:

for k = 1:numel(x)
    subplot(1,2,1)
    plot(x(k),y(k),'-') %// Choose your own marker here

    subplot(1,2,2)
    plot(x(1:k),y(1:k))

    %// MATLAB pauses for 0.001 sec before moving on to execue the next 
    %%// instruction and thus creating animation effect
    pause(0.001);     
end
1
votes
% Easiest way
x = rand(1, 11); y = rand(1, 11);
z = rand(1, 11); a = rand(1, 11);
figure
for i = 1 : 10
    subplot(211)
    plot(x(i : i+1), y(i : i+1), '.-k');
    hold on; % include this if you want to show plot history

    subplot(212)
    plot(z(i : i+1), a(i : i+1), '.-k');

    drawnow;
    pause(0.1);
end

% If you don't want to call "plot" interatively
x = rand(1, 11); y = rand(1, 11);
z = rand(1, 11); a = rand(1, 11);
figure
subplot(211)
p1 = plot(NaN, NaN, 'marker', 'o');
subplot(212)
p2 = plot(NaN, NaN, 'marker', 'd');
for i = 1 : 10
      set(p1, 'xdata', x(i : i+1), 'ydata', y(i : i+1));
      set(p2, 'xdata', z(i : i+1), 'ydata', a(i : i+1));

      drawnow;
      pause(0.1);
end
0
votes

First define your plot as a construct, so p1 = plot(x,y). Then you set up your loop and in the loop your write

  set(p1,'YData',y);

This will update the plot p1s YData which is y. If you want to see it in an animated form just add a pause(0.1) %seconds after the set.