Faster way to make movies than drawing figures
31 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Rik
2020-6-26
编辑:Rik
2020-6-26
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 个评论
Walter Roberson
2020-6-26
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 个评论
Walter Roberson
2020-6-26
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!