recording a plot as a video & including a suitable legend

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

 Accepted Answer

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)

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

Thanks, that is really useful. I perhaps shpould have included that k = 1 and increments by 1 until a certain condition is met. At which point it stops. So there aren't explicitly ~30 steps
I noticed your while loop, but I just did a for loop to make it easy for myself to provide an example. I mainly wanted to emphasis the use of the axes handle to update XData and YData.
If this answers your question, I would appreciate it if you would accept this answer.
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.
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

Sign in to comment.

Categories

Products

Release

R2022b

Asked:

on 27 Apr 2023

Commented:

on 27 Apr 2023

Community Treasure Hunt

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

Start Hunting!