recording a plot as a video & including a suitable legend
조회 수: 32 (최근 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)
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!