필터 지우기
필터 지우기

Create and Save multiple animated plots on the same axes

조회 수: 3 (최근 30일)
Japnit Sethi
Japnit Sethi 2019년 12월 24일
댓글: Walter Roberson 2019년 12월 26일
So I created a plot comparing my P, I, PID,PI controllers and the open loop system on the same figure !!
I want to create an animated plot of all these in the same figure and then record a video.
All the simulations run until 1.4 seconds, but the number of points in each time period is different.
Below is code for plotting multiple plots together
figure()
plot(t_plant, squeeze(y_plant), 'LineWidth', 2);
hold on
grid on
plot(t_P, squeeze(y_P), 'LineWidth', 2);
plot(t_I, squeeze(y_I), 'LineWidth', 2);
plot(t_PI, squeeze(y_PI), 'LineWidth', 2);
plot(t_PID, squeeze(y_PID), 'LineWidth', 3);
% 2% settling time refrence line
hRef1=refline(0,1);
set(hRef1,'color','b'); % Max horizontal
hRef2=refline(0,1.02);
set(hRef2,'color','r','linestyle','--'); % Max horizontal
hRef3=refline(0,0.98);
set(hRef3,'color','r','linestyle','--'); % Min horizontal
hold off
title('Step Response');
xlabel('Time (sec)');
ylabel('Step Response');
h = legend('Open-loop Plant', 'P', 'I', 'PI', 'PID', '1 sec', '2% S.T range', '2% S.T range');
set(h, 'Location','southeast');
ylim([0 1.5]);
This was the code I used for plotting multiple animated plots together and saving it to a video:
% Create Animated Plot
y_P_Final = squeeze(y_P);
y_I_Final = squeeze(y_I);
y_PI_Final = squeeze(y_PI);
y_PID_Final = squeeze(y_PID);
y_plant_Final = squeeze(y_plant);
% Make some data
% Initialize video
myVideo = VideoWriter('OverallCompensatorComparison'); % open video file
myVideo.FrameRate = 10; % can adjust this, 5 - 10 works well for me
open(myVideo)
% Plot in a loop and grab frames
for i=1:1:length(t_plant)
plot(t_plant(1:i), y_plant_Final(1:i), 'LineWidth', 2);
plot(t_P(1:i), y_P_Final(1:i), 'LineWidth', 2);
plot(t_I(1:i), y_I_Final(1:i), 'LineWidth', 2);
plot(t_PI(1:i), y_PI_Final(1:i), 'LineWidth', 2);
plot(t_PID(1:i), y_PID_Final(1:i), 'LineWidth', 3);
ylim([0, 1.5])
xlim([0, 1.4])
pause(0.01) % Pause and grab frame
frame = getframe(gcf); %get frame
writeVideo(myVideo, frame);
end
Data points are all included in the attached .mat file.

답변 (1개)

Walter Roberson
Walter Roberson 2019년 12월 24일
find the min() and max() over all of the times, and linspace() up a time vector over that range. Now go through the 5 variables and interp1() them to the common time base. Now you can plot them over the common time base. (I suggest using animatedline() )
  댓글 수: 3
Japnit Sethi
Japnit Sethi 2019년 12월 26일
Was finally able to create a video of multiple plots in one figure
% Create Animated Plot
t_animation = linspace(0, 1.4);
t_animation = t_animation';
y_plant_Final = squeeze(y_plant);
y_P_Final = squeeze(y_P);
y_I_Final = squeeze(y_I);
y_PI_Final = squeeze(y_PI);
y_PID_Final = squeeze(y_PID);
% Normalising to same time step of 0 to 1.4 sec for each output
y_plant_Final_animation = interp1(t_plant(:,1), y_plant_Final(:,1), t_animation(:,1));
y_P_Final_animation = interp1(t_P(:,1),y_P_Final(:,1), t_animation(:,1));
y_I_Final_animation = interp1(t_I(:,1), y_I_Final(:,1), t_animation(:,1));
y_PI_Final_animation = interp1(t_PI(:,1), y_PI_Final(:,1), t_animation(:,1));
y_PID_Final_animation = interp1(t_PID(:,1), y_PID_Final(:,1), t_animation(:,1));
myVideo = VideoWriter('OverallCompensatorComparison'); % open video file
myVideo.FrameRate = 50;
open(myVideo)
figure
h1 = animatedline('Color','b','LineWidth', 2.5);
h2 = animatedline('Color','r','LineWidth', 2.5);
h3 = animatedline('Color', '#EDB120','LineWidth', 2.5);
h4 = animatedline('Color','#7E2F8E','LineWidth', 2.5);
h5 = animatedline('Color','g','LineWidth', 2.5);
for k = 1:length(t_animation)
addpoints(h1, t_animation(k),y_plant_Final_animation(k));
addpoints(h2, t_animation(k),y_P_Final_animation(k));
addpoints(h3, t_animation(k),y_I_Final_animation(k));
addpoints(h4, t_animation(k),y_PI_Final_animation(k));
addpoints(h5, t_animation(k),y_PID_Final_animation(k));
% 2% settling time refrence line
hRef1=refline(0,1);
set(hRef1,'color','b'); % Max horizontal
hRef2=refline(0,1.02);
set(hRef2,'color','r','linestyle','--'); % Max horizontal
hRef3=refline(0,0.98);
set(hRef3,'color','r','linestyle','--'); % Min horizontal
pause(0.01) % Pause and grab frame
frame = getframe(gcf); % get frame
writeVideo(myVideo, frame);
title('Step Response');
xlabel('Time (sec)');
ylabel('Step Response');
h = legend('Open-loop Plant', 'P', 'I', 'PI', 'PID', '1 sec', '2% S.T range', '2% S.T range');
set(h, 'Location','southeast');
ylim([0 1.5]);
xlim([0 1.4]);
end
% In order to maximize the figure window in Windows
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
close(myVideo)
drawnow
Walter Roberson
Walter Roberson 2019년 12월 26일
t_animation = linspace(0, 1.4);
That is a row vector
y_plant_Final_animation = interp1(t_plant(:,1), y_plant_Final(:,1), t_animation(:,1));
There, t_animation(:,1) is referring to t_animation as if it were a column vector or a 2D array with the interesting information down the first column. But because it is a row vector, t_animation(:,1) is the scalar which is the initial value, 0 . It seems more likely to me that you would be wanting to use t_animation or t_animation(1,:)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by