Speed up frames per second

43 ビュー (過去 30 日間)
MADRobot
MADRobot 2020 年 6 月 29 日
コメント済み: Adam Danz 2020 年 7 月 2 日
I'm trying to create a video/animation using a large amount of data. I have currently collated all 'marker' points and joined them using plot3, but I want to speed up the animation, as it's really slow. I have tried to create a video, but it kept appearing as empty for some reason?
Here is a sample of my code:
for i = 1:1033
RPtoT = [RASIS(i,:); RT2(i,:); RT3(i,:)];
idx = [1 2 3 1];
plot3(RPtoT(idx,1), RPtoT(idx,2), RPtoT(idx,3), '-o', 'Color', 'g');
axis equal
hold on
LPtoT = [LASIS(i,:); LT0(i,:); LT1(i,:)];
idx = [1 2 3 1];
plot3(LPtoT(idx,1), LPtoT(idx,2), LPtoT(idx,3), '-o', 'Color', 'g');
RT = [RT0(i,:); RT1(i,:); RT2(i,:); RT3(i,:)];
idx = [1 2 3 1 4 2 3 4];
plot3(RT(idx,1),RT(idx,2),RT(idx,3),'-o', 'Color', 'g');
LT = [LT0(i,:); LT1(i,:); LT2(i,:); LT3(i,:)];
idx = [1 2 3 1 4 2 3 4];
plot3(LT(idx,1),LT(idx,2),LT(idx,3),'-o', 'Color', 'g');
hold off
Mod = figure(1);
fighandle = gcf();
F = getframe(i);
end
I've tried using the movie function but I haven't noticed it doing anything since I added it to my code.
n = [1, 1000, 10:1000]
movie(F(i),n, 200)
Please bear in mind I'm not an experienced Matlab user, I'm still relatively new. Any help would be much appreciated! Thanks in advance!
  5 件のコメント
Voss
Voss 2020 年 7 月 2 日
I'm not sure. What does the error say?
Adam Danz
Adam Danz 2020 年 7 月 2 日
@MADRobot, always include the entire error message when reporting an error.
When there is no input to getframe() it uses the current axes: getframe(gca).
If there is no figure, it will create one.
I'm guessing that the error is either an indexing error with F(i) or the error is unrelated to this line.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2020 年 6 月 30 日
編集済み: Adam Danz 2020 年 7 月 1 日
There are several steps you can take to speed up the animation.
Optimize the loop
See inline comments for recommendations. Some of these steps will be eliminated if you choose to implement the next section.
for i = 1:1033
RPtoT = [RASIS(i,:); RT2(i,:); RT3(i,:)];
idx = [1 2 3 1]; % this can be moved outside of the loop
plot3(RPtoT(idx,1), RPtoT(idx,2), RPtoT(idx,3), '-o', 'Color', 'g');
axis equal % this can be moved outside of the loop
hold on % this can be moved outside of the loop
LPtoT = [LASIS(i,:); LT0(i,:); LT1(i,:)];
idx = [1 2 3 1]; % this can be removed since it's the same as the first "idx" variable.
plot3(LPtoT(idx,1), LPtoT(idx,2), LPtoT(idx,3), '-o', 'Color', 'g');
RT = [RT0(i,:); RT1(i,:); RT2(i,:); RT3(i,:)];
idx = [1 2 3 1 4 2 3 4]; % this can be moved outside of the loop
plot3(RT(idx,1),RT(idx,2),RT(idx,3),'-o', 'Color', 'g');
LT = [LT0(i,:); LT1(i,:); LT2(i,:); LT3(i,:)];
idx = [1 2 3 1 4 2 3 4]; % this can be removed since it's the same as the previous "idx" variable.
plot3(LT(idx,1),LT(idx,2),LT(idx,3),'-o', 'Color', 'g');
hold off % is this really needed? If so, move it outside, after the loop.
Mod = figure(1); % this should be moved outside of the loop. Are you even using the variable?
fighandle = gcf(); % this is redundant; it's the same as "Mod", I think, and it can be moved outside of the loop; Are you even using this variable?
F = getframe(i); % Why are you providing i as an input?
end
Update line object data rather than adding additional line objects
Every time you call plot3() or any plotting function, you create a new line object. With 1033 iterations, that creates LOTS of objects and that will eventually slow down the execution. Instead, update the XData, YData, and XData properties of the line objects within the loop.
Here's an example you can apply to your loop. It only adjusts 1 plot3 object. It looks like you'll need to do this for 4 plot3 objects. You may also need to adjust the indexing to fit your data.
% DEMO
% Create axes and 1 plot3() object
ax = axes();
hold(ax,'on')
h1 = plot3(ax, nan,nan,nan, '-o', 'Color', 'g');
% Update the plot3 object
idx = [1 2 3 1];
for i = 1:10
RPtoT = [1:5; 1:5; rand(1,5)];
h1.XData = [h1.XData, RPtoT(idx,1).']; % ignore warning or learn howo supress warnings
h1.YData = [h1.YData, RPtoT(idx,2).']; % ignore warning or learn howo supress warnings
h1.ZData = [h1.ZData, RPtoT(idx,3).'];
end
% Remove initial nan
h1.XData(1) = [];
h1.YData(1) = [];
h1.ZData(1) = [];
Update the animation every few iterations instead of every iteration
If your animation is stil slow, update the animation every few iterations by using a conditional statement.
% outside of your loop
nIterations = 1033;
updateIterations = 1 : 3 : nIterations; % every 3 iterations
fig = gcf(); % better yet, fig = figure(); when you create the figure.
F(numel(updateIterations)) = struct('cdata',[],'colormap',[]);
% at the bottom of your loop
for i = 1:nIterations
% [SKIPPING CODE.....]
[captureFrame, frameNumber] = ismember(i,updateIterations);
if captureFrame
F(frameNumber) = getframe(fig);
end
end
Alternatively you can capture all of the frames and try to use the fps input of the movie() function.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by