julianfperez
julianfperez

Reputation: 1744

Improve animation rendering in Matlab

I have written a code to create an animation (satellite movement around the Earth). When I run it, it works fine. However, when it is modified to be part of a code much more complex present in a Matlab GUI, the results produced changes (mainly because of the bigger number of points to plot). I also have noticed that if I use the OpenGL renderer the movement of the satellite is quicker than when the other renderers (Painters and Zbuffer) are used. I do not know if there are further possibilities to achieve an improvement in the rendering of the satellite movement. I think the key is, perhaps, changing the code that creates the actual position of the satellite (handles.psat) and its trajectory along the time (handles.tray)

handles.tray = zeros(1,Fin);
handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
    'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b');
...
while (k<Fin)
            az = az + 0.01745329252;
            set(hgrot,'Matrix',makehgtform('zrotate',az));
            handles.tray(k) = line([Y(k-1,1) Y(k,1)],[Y(k-1,2) Y(k,2)],...
                [Y(k-1,3) Y(k,3)],...
        'Color','red','LineWidth',3);
            set(handles.psat,'XData',Y(k,1),'YData',Y(k,2),'ZData',Y(k,3));
            pause(0.02); 
            k = k + 1;

            if (state == 1)
                state = 0;
                break;
            end
            end
...

Upvotes: 0

Views: 379

Answers (2)

tmpearce
tmpearce

Reputation: 12693

You've used the typical tricks that I use to speed things up, like precomputing the frames, setting XData and YData rather than replotting, and selecting a renderer. Here are a couple more tips though:

1) One thing I noticed in your description is that different renderers and different complexities changed how fast your animation appeared to be running. This is often undesirable. Consider using the actual interval between frames (i.e. use tic; dt = toc) to calculate how far to advance the animation, rather than relying on pause(0.2) to generate a steady frame rate.

2) If the complexity is such that your frame rate is undesirably low, consider replacing pause(0.02) with drawnow, or at least calculate how long to pause on each frame.

3) Try to narrow down the source of your bottleneck a bit further by measuring how long the various steps take. That will let you optimize the right stage of the operation.

Upvotes: 1

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

Did you consider to apply a rotation transform matrix on your data instead of the axis?
I think <Though I haven't checked it> that it can speedup your code.

Upvotes: 1

Related Questions