HOW TO OVERWRITTE PLOTS IN A FOR LOOP

5 visualizzazioni (ultimi 30 giorni)
carlos santiyan esteban
carlos santiyan esteban il 14 Lug 2021
Risposto: Rishik Ramena il 19 Lug 2021
I want to overwritte plots coming from other files in a FOR loopm, in order so that each plot generated is held and the next one in the loop is also in drawn in the plot. I also want that the legend changes for every loop. I write the code:
% calculates the acceleration of a vehicle using the two codes:
vehicle_2021_v_p
electric_drive_2021
close all
%wm: motor speed [rad/s]
wm=n*2*pi/60;
for i= 1: length (rt)
%v_m: vehicle speed using speed discretization of motor [m/s]
v_m=wm/rt(i)*rw;
%f_m: tractive effort(i) using the speed discretization of the motor [N]
f_m=t*rt(i)/rw;
%ft: tractive effort(i) using vehicle speed discretization step dv [N]
ft=interp1(v_m,f_m,v);
hold on
figure
subplot(2,1,1)
plot(vkph,ft, vkph, fl, 'linewidth',2)
grid
xlabel ('vehicle speed [km/h]')
ylabel ('[N]')
title ('FORCE')
legend ('TRACTIVE EFFORT','LOAD')
subplot(2,1,2)
plot(vkph,ft.*v/1000, vkph, fl.*v/1000, 'linewidth',2)
grid
xlabel ('vehicle speed [km/h]')
ylabel ('[kW]')
title ('POWER')
legend ('TRACTIVE EFFORT','LOAD')
%tl_v: load torque using vehicle speed sampling [Nm]
tl_v=fl*rw/rt(i);
%n_v: motor speed using vehicle speed sampling [rpm]
n_v=v/rw*rt(i)*60/2/pi;
%tl: load torque using motor speed sampling [Nm]
tl=interp1(n_v,tl_v,n);
figure
subplot(2,1,1)
plot(n(:,i),t(:,i), n(:,i), tl(:,i), 'linewidth',2)
grid
xlabel ('motor speed [rpm]')
ylabel ('[Nm]')
title ('TORQUE')
legend ('MOTOR','LOAD')
subplot(2,1,2)
plot(n,p/1000, n, tl.*n*2*pi/60/1000, 'linewidth',2)
grid
xlabel ('motor speed [rpm]')
ylabel ('[kW]')
title ('POWER')
legend ('MOTOR','LOAD')
%% ACCELERATION CALCULATION %%%
%facc: acceleration force [N]
facc=ft-fl;
%i_pos_acc: indexes of positive acceleration
i_pos_acc=find(facc>0);
%i_max_speed: index of maximum speed
i_max_speed=length(i_pos_acc);
%v_max: top speed [km/h]
v_max =v(i_max_speed)*3.6
%acc: acceleration [m/s^2]
acc=facc(i_pos_acc)/mg;
%max_acc: maximum accelration [m/s^2]
max_acc=max(acc);
%g_max: maximum accelration [g]
g_max=max_acc/9.81;
%acc=dv/dt
%dt: vector of delta time for each dv [s]
dt=dv./acc;
%time: acceleration time [s]
time=cumsum(dt);
%d_dst: delta distance [m]
d_dst=v(i_pos_acc).*dt;
%distance: total distance [m]
distance=cumsum(d_dst);
figure(i)
subplot(3,1,1)
plot(time, acc, 'linewidth',2)
grid
xlim([0 200])
title('ACCELERATION')
xlabel ('time [s]')
ylabel ('[m/s^2]')
subplot(3,1, 2)
plot(time, v(i_pos_acc)*3.6, 'linewidth',2)
grid
xlim([0 200])
title('SPEED')
xlabel ('time [s]')
ylabel ('[km/h]')
subplot(3,1, 3)
plot(time, distance, 'linewidth',2)
grid
xlim([0 200])
title('DISTANCE')
xlabel ('time [s]')
ylabel ('[m]')
end

Risposte (1)

Rishik Ramena
Rishik Ramena il 19 Lug 2021
Since you want to update the plot keeping the old data intact, add a pause after the plot and 'hold on' after the first plot. a sample code is provided for your reference.
figure; title('Plot loops');
A = rand(1,10);
B = rand(1,10);
N = length(A);
for idx=1:N
plot(A(1:idx),B(1:idx), '*-');
legend(['test' int2str(idx)]);
hold on
pause(2)
end
hold off

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by