recording a plot as a video & including a suitable legend
26 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Kevin Holly
2023-4-27
编辑:Kevin Holly
2023-4-27
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)
更多回答(1 个)
Kevin Holly
2023-4-27
编辑:Kevin Holly
2023-4-27
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!