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