MATLAB: Create a video of single plot with 3 trajectories each following another

figureMATLABplotvideo

So I am working on a project on Model Reference Adaptive controls !! Here I have 3 trajectories :
  • Desired Trajectory
  • Reference Trajectory
  • Actual Plant's trajectory
Theoretically in my system the Reference trajectory follows the desired trajectory, and the Actual Plant's trajectory follows the Reference trajecotry !!
I know how to create a video/gif such that the 3 trajectories are being plotted at the same time !!
Question:
I want ask if there is a way if I can create a video that first show the Desired Trajectory being plotted then the Reference trajectory and then finally the Actualy Plant's trajectory all on the same figure

Best Answer

I was eventually able to figure it out myself !! Just in case anybody is looking for a solution to a similar problem:
myVideo = VideoWriter('Trajectories'); % open video file
myVideo.FrameRate = 200;
open(myVideo)
figure
h1 = animatedline('Color','b', 'LineStyle', '-', 'LineWidth', 4);
h2 = animatedline('Color','r','LineStyle', '--', 'LineWidth', 2.5);
h3 = animatedline('Color','g','LineStyle', '-.', 'LineWidth', 1);
for k = 1:length(tout)
addpoints(h1, tout(k),xd(k));
pause(10) % Pause and grab frame


frame = getframe(gcf); % get frame


writeVideo(myVideo, frame);
title('Desired, Reference and Actual Trajectories');
xlabel('Time (sec)');
ylabel('Trajectories');
ylim([-11 11]);
xlim([0 max(tout)]);
end
for k = 1:length(tout)
addpoints(h2, tout(k),xr1(k));
pause(10) % Pause and grab frame
frame = getframe(gcf); % get frame
writeVideo(myVideo, frame);
title('Desired, Reference and Actual Trajectories');
xlabel('Time (sec)');
ylabel('Trajectories');
ylim([-11 11]);
xlim([0 max(tout)]);
end
for k = 1:length(tout)
addpoints(h3, tout(k),x1(k));
pause(10) % Pause and grab frame
frame = getframe(gcf); % get frame
writeVideo(myVideo, frame);
title('Desired, Reference and Actual Trajectories');
xlabel('Time (sec)');
ylabel('Trajectories');
h = legend('Desired', 'Reference', 'Actual');
set(h, 'Location','southeast');
ylim([-11 11]);
xlim([0 max(tout)]);
end
% In order to maximize the figure window in Windows
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
close(myVideo)
drawnow