MATLAB: Matlab keeps overwritting all figures

figureMATLABplot

My matlab for-loop is plotting the new code on both figures. I did call a new figure in the last part however the for loop plots everything on both the 2 figures. How do I make it plot on the newest figure only?
%%b)
%The charge would be 1.6e-10 C. Dimer would be 2*1.6e-19 C. Trimer would be
%3*1.6e-19 C. N units would have N*1.6e-19 C of charges.
array=1000:1:1010; %Create the base array
charge=array.*(1.6e-19); %multiply it by the charge
%%c)
visw=0.001; %viscocity in water is 10^-3 kg/s
r=array.*(0.1*10^-9); %find the radius of the N unit polymers
f=r.*(6*pi*visw); %frictional coefficient =6*pi*viscosity*r
%%d)
mobility=charge./f; %mobility = q/f

%it will work super bad. All the values in the mobility array are basically
%the same, which will make the polymers nearly impossible to separate.
%%e)
f1000=2.5e-8; %Frictional coefficient in the 4% acrylamide gel N=1000
newf=(f1000/1000000).*(array.^2); %f for all 1000-1010 DNA
%%f)mobility= q/f
newmobility = charge./newf; %mobility = q/f
%%g)
E=20000; %E=200V/cm = 20000V/m
velocity = newmobility.*E; %U=Uep*E = q/f *E
%Capillary Electrophoresis model
x0=0; %initial x position of the components
v1 = velocity (1)*100; %velocity of a DNA unit DNA in cm/s
v2 = velocity (2)*100;
v3 = velocity (3)*100;
v4 = velocity (4)*100;
v5 = velocity (5)*100;
v6 = velocity (6)*100;
v7 = velocity (7)*100;
v8 = velocity (8)*100;
v9 = velocity (9)*100;
v10 = velocity (10)*100;
time = linspace(0,1668,1669); %load an array of time points
pos1 = x0 +v1*time; %calulate the position of each species at each time
pos2 = x0 +v2*time;
pos3 = x0 +v3*time;
pos4 = x0 +v4*time;
pos5 = x0 +v5*time;
pos6 = x0 +v6*time;
pos7 = x0 +v7*time;
pos8 = x0 +v8*time;
pos9 = x0 +v9*time;
pos10 = x0 +v10*time;
posy = linspace(0,0,1669);
figure(1)
%plot (pos1,posy,'.',pos2,posy,'.',pos3,posy,'.',pos4,posy,'.',pos5,posy,'.',pos6,posy,'.',pos7,posy,'.',pos8,posy,'.',pos9,posy,'.',pos10,posy,'.');
plot (time,pos1,'.', time, pos2, '.',time,pos3,'.',time,pos4,'.',time,pos5,'.',time,pos6,'.',time,pos7,'.',time,pos8,'.',time,pos9,'.',time,pos10,'.')
xlabel('time')
ylabel('position')
title('position vs time')
%%h)at the end we need 0.2mm which is 0.0002 m separation
%suppose the length is L. the time the molecule travels through L is L/v.
%we want molecules to have a travel distance with 0.0002m, which means that
%when 1 reaches the end, 9 and 10 are 0.0002m away from each other. At this
%time t=L/v1, v9*t-v10*t=0.0002. solve the formula.
%velocity (1) %v1= 1.2800e-04 m/s
%velocity (9) %v9= 1.2698e-04 m/s
%velocity (10) %v10= 1.2686e-04 m/s
%solve the equation we get L=0.2133333333 m
%time for 1 to come out is L/v1=1666.6666 s
%%i) with L=0.2133333333m, E=V/L= 20000V/m so V=E*L= 4266.66V
%%j) Model
% loop to plot the new position on the same axes.
pause on;
figure
for k=1:1668;
plot(pos1 (k),posy(k),'.',pos2(k),posy(k),'.',pos3(k),posy(k),'.',pos4(k),posy(k),'.',pos5(k),posy(k),'.',pos6(k),posy(k),'.',pos7(k),posy(k),'.',pos8(k),posy(k),'.',pos9(k),posy(k),'.',pos10(k),posy(k),'.');
axis ([0 25, -1 1]);
xlabel ('position in cm');
title ('velocity in 1cm/s base for zooming');
pause(0.00000000001);
end

Best Answer

In every graphics operation, tell MATLAB specifically which object it needs to act on.
fig1 = figure(1);
ax1 = axes('Parent', fig1);
plot(ax1, time, pos1, '.', time, pos2, '.', time, pos3, '.', time, pos4, '.', time, pos5, '.', time, pos6, '.', time, pos7, '.', time, pos8, '.', time, pos9, '.', time, pos10, '.');
xlabel(ax1, 'time')
ylabel(ax1, 'position')
title(ax1, 'position vs time')
fig2 = figure();
ax2 = axes('Parent', fig2);
for k=1:1668;
plot(ax2, pos1 (k), posy(k), '.', pos2(k), posy(k), '.', pos3(k), posy(k), '.', pos4(k), posy(k), '.', pos5(k), posy(k), '.', pos6(k), posy(k), '.', pos7(k), posy(k), '.', pos8(k), posy(k), '.', pos9(k), posy(k), '.', pos10(k), posy(k), '.');
axis(ax2, [0 25, -1 1]);
xlabel(ax2, 'position in cm');
title(ax2, 'velocity in 1cm/s base for zooming');
pause(0.00000000001);
end