Faster way to make movies than drawing figures

23 views (last 30 days)
I am trying to create 60 fps movies in matlab, they should only be a few seconds long but drawing each plot takes an excrutiatingly long time. I can't really reduce the fps because things move really quickly and I don't want it to look jerky. Turning the figure visibility off doesn't give any speed up and just stops me knowing how close to being done the process is.
Is there no faster way to make a movie based on plotted data?
F(num_frames) = struct('cdata',[],'colormap',[]);
for frame = 1:num_frames
cla(ax)
hold(ax ,'on')
grid(ax, 'on')
xlim(ax, [-axlim axlim])
ylim(ax, [-axlim axlim])
plot(ax, [0, x(frame, 1)], [0, y(frame, 1)])
for i = 1:3
plot(ax, [x(frame, i), x(frame, i+1)], ...
[y(frame, i), y(frame, i+1)])
end
drawnow
F(frame) = getframe(f);
end

Answers (2)

Rik
Rik on 26 Jun 2020
Edited: Rik on 26 Jun 2020
You should initialize the graphics objects outside the loop and then modify the properties inside the loop.
The dynamic expansion of F could still be a bottleneck though. Also note that you are using j as an index there, so you can speed this code up by only executing the last frame iteration.
F(num_frames) = struct('cdata',[],'colormap',[]);
cla(ax)
hold(ax ,'on')
grid(ax, 'on')
xlim(ax, [-axlim axlim])
ylim(ax, [-axlim axlim])
h_line1=plot(ax, [0, x(1, 1)], [0, y(1, 1)]);
for i = 1:3
h_line_i(i)=plot(ax, [x(1, i), x(1, i+1)], [y(1, i), y(1, i+1)]);
end
for frame = 1:240
set(h_line1,'XData',[0, x(frame, 1)],'YData',[0, y(frame, 1)])
for i = 1:3
set(h_line_i(i),...
'XData',[x(frame, i), x(frame, i+1)], ...
'YData',[y(frame, i), y(frame, i+1)])
end
drawnow
F(j) = getframe(f);
end
  4 Comments
Conor Marsh
Conor Marsh on 26 Jun 2020
Exactly what I'm hoping to find! There must be a better way to create a movie...
Rik
Rik on 26 Jun 2020
Untested: some options in export_fig may have a better performance.

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Jun 2020
You are doing simple plot() calls, and most of your time is spent capturing the frame.
If you have Computer Vision toolbox, then instead of using getframe, use insertShape https://www.mathworks.com/help/vision/ref/insertshape.html and possibly insertText() as well, to "draw" into an array that you have initialized to your desired background color, after which you can copy the array as your "frame", without doing getframe()
  5 Comments
Walter Roberson
Walter Roberson on 26 Jun 2020
bg = ones(Rows, Columns, 3); %white
colors = parula(64); %colors to rotate through
ncolor = size(colors,1);
cidx = 0;
for frame = 1:(3*240)
I = bg;
for i = 1 : 3
cidx = 1 + mod(cidx, ncolor); %next color from list
M = [x(frame, i), y(frame,i), x(frame, i+1), y(frame, i+1)];
I = insertShape(I, 'Line', M, 'Color', colors(cidx,:));
end
end
except improved to have the other line as well.
Conor Marsh
Conor Marsh on 29 Jun 2020
I was hoping to test this today but my admin rights won't let me install the computer vision toolbox... when my IT services get back to me I'll get back to you.
Thank you very much for the help!

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!