recording a plot as a video & including a suitable legend
26 views (last 30 days)
Show older comments
I have a plot that is drawing itself as it is being calculated. I would like to record it as the speed it is being calculated. I would also like to add a legend, however due to the way it is calculate, one of the data sets is one item on the legend, the second data set becomes 36 items on the legend. As can be seen on the image.
hold
box on
grid on
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1])
plot(OXmcdata(OXmcdata(:,1) > 0,1)/100,OXmcdata(OXmcdata(:,1) > 0,2),'LineWidth',2,'Color',[0 0 0]);
set(gca,'FontSize',20)
xlabel('$\bar{\epsilon}_1$','FontSize',32,'Interpreter','latex')
ylabel('$\bar{\sigma}_1$ (MPa)','FontSize',32,'Interpreter','latex')
%
while continue_loading
%
%
% code goes here
%
%
if continue_loading % add point on chart
plot(eps_bar(1,k),s_bar(1,k),'o','MarkerSize',12,'MarkerFaceColor',[1 0 0])
pause(0.1)
exportgraphics(gcf,'Z:\MATLAB\figures\MT_example.gif','Append',true);
writerObj = VideoWriter('Z:\MATLAB\figures\example.avi');
writerObj.FrameRate = 60;
open(writerObj);
end
% %
%
% more code goes here
I have two commands, one that records as a .gif, but this plays back too quickly and I cannot control the playback once embed in Powerpoint. The second command is to record it as an .avi. However it does not like the .avi command.
Please see image regarding legend issue
Any help is welcome
Thanks
0 Comments
Accepted Answer
Kevin Holly
on 27 Apr 2023
Edited: Kevin Holly
on 27 Apr 2023
continue_loading = 1;
v = VideoWriter('Z:\MATLAB\figures\example.avi');
v.FrameRate = 60;
open(v)
%
hold
box on
grid on
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1])
plot(OXmcdata(OXmcdata(:,1) > 0,1)/100,OXmcdata(OXmcdata(:,1) > 0,2),'LineWidth',2,'Color',[0 0 0]);
set(gca,'FontSize',20)
xlabel('$\bar{\epsilon}_1$','FontSize',32,'Interpreter','latex')
ylabel('$\bar{\sigma}_1$ (MPa)','FontSize',32,'Interpreter','latex')
title({'MT homogenisation comparison with Ox/Ox-CMC'});
h = plot(eps_bar(1,k),s_bar(1,k),'o','MarkerSize',12,'MarkerFaceColor',[1 0 0]);
%
while continue_loading
%
k = k + 1;
%
% Some code
if continue_loading % add point on chart
pause(0.1)
legend
h.XData = [h.XData,eps_bar(1,k)];
h.YData = [h.YData,s_bar(1,k)];
F = getframe(h.Parent);
% img = F.cdata;
writeVideo(v,F)
%
%
end
end
close(v)
More Answers (1)
Kevin Holly
on 27 Apr 2023
Edited: Kevin Holly
on 27 Apr 2023
Legend Problem
Making up data for example
eps_bar = rand(1,30);
s_bar = rand(1,30);
k=1;
You could get the handle of the red plot as such:
h = plot(eps_bar(1,k),s_bar(1,k),'o','MarkerSize',12,'MarkerFaceColor',[1 0 0]); % Note you could use scatter instead
legend
Then you can use the handle to update the subfield XData and YData as such:
for k = 2:30
h.XData = [h.XData,eps_bar(1,k)];
h.YData = [h.YData,s_bar(1,k)];
end
Saving Video Problem
Define Video file to save
v = VideoWriter('Z:\MATLAB\figures\example.avi');
v.FrameRate = 60;
Open the file for writing
open(v)
Write frames to video
while continue_loading
if continue_loading % add point on chart
h.XData = [h.XData,eps_bar(1,k)];
h.YData = [h.YData,s_bar(1,k)];
F = getframe(h);
% img = F.cdata;
writeVideo(v,F)
end
end
Close the file.
close(v)
4 Comments
See Also
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!