recording a plot as a video & including a suitable legend

22 vues (au cours des 30 derniers jours)
A Poyser
A Poyser le 27 Avr 2023
Commenté : A Poyser le 27 Avr 2023
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

Réponse acceptée

Kevin Holly
Kevin Holly le 27 Avr 2023
Modifié(e) : Kevin Holly le 27 Avr 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)

Plus de réponses (1)

Kevin Holly
Kevin Holly le 27 Avr 2023
Modifié(e) : Kevin Holly le 27 Avr 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 commentaires
A Poyser
A Poyser le 27 Avr 2023
I am in the process of implementin the suggestions. I will be happy to accept the answer once I have mangaed to convert your concepts into my code. It might take me a little while as I am writting a presentation and two reports concurrently. Hopefully you can give me a little head room.
A Poyser
A Poyser le 27 Avr 2023
I tried implimenting the code and it seems to give an error in the rcording
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'});
%
while continue_loading
%
k = k + 1;
%
% Some code
if continue_loading % add point on chart
h = plot(eps_bar(1,k),s_bar(1,k),'o','MarkerSize',12,'MarkerFaceColor',[1 0 0]);
pause(0.1)
legend
h.XData = [h.XData,eps_bar(1,k)];
h.YData = [h.YData,s_bar(1,k)];
F = getframe(v);
img = F.cdata;
writeVideo(v,F)
%
%
end
end
close(v)
gives this error
Error using getframe
A valid figure or axes handle must be specified
Error in OX_OX_example (line 138)
F = getframe(v);
It also seems that (h) doesn't seem to store the previous bit of data so the legend still appears as multiple discrete items
Thanks for your help

Connectez-vous pour commenter.

Produits


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by