Create and Save multiple animated plots on the same axes
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257103/image.jpeg)
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.
0 Kommentare
Antworten (1)
Walter Roberson
am 24 Dez. 2019
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 Kommentare
Walter Roberson
am 26 Dez. 2019
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,:)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!