Help speeding up a plot
2 views (last 30 days)
Show older comments
I have a plot of a planet, satellites and their orbits. I have 32 satellites and would like to plot all of them however doing this leads to massive lag in the plot. I'd like to speed up the plot because I have further things I need to do after plotting the satellites. Can anyone help/suggest improvements so that the full 32 satellites run as smooth as these 4?
Full code can be provided if required.
This is the plotting code I'm using, currently only for plotting 4 satellites (speed is fine), for 32 satellites, loops for k would change to 1:8, 9:16, 17:24, 25:32;
%PLOT
%Create Mars as a surface
marsterrain = imread('mars.jpg');
[X, Y, Z] = sphere(20);
mars = mesh(X*Mars.r, Y*Mars.r, Z*Mars.r);
set(mars,'FaceColor','texturemap','cdata',marsterrain,'edgecolor','none');
hold on
%Draw Orbits
Orbit1 = animatedline(POS(1,1), POS(33,1), POS(65,1), 'color', [1 0.5 0]);
Orbit2 = animatedline(POS(9,1), POS(41,1), POS(73,1), 'color', [0 1 0]);
Orbit3 = animatedline(POS(17,1), POS(49,1), POS(81,1), 'color', [0.2 0.6 1]);
Orbit4 = animatedline(POS(25,1), POS(57,1), POS(89,1), 'color', [1 0 0]);
%Set axis limits
axis equal
xlim([-a, a]);
ylim([-a, a]);
zlim([-a, a]);
set(gcf, 'color', [0 0 0])
axis off
hold on
%Plot over time
for j = 1:length(t)
%Rotate Mars
rotate(mars, [0 0 1], (rad2deg(Mars.th)));
%Draw orbits from leading satellite
addpoints(Orbit1, POS(1,j), POS(33,j), POS(65,j));
addpoints(Orbit2, POS(9,j), POS(41,j), POS(73,j));
addpoints(Orbit3, POS(17,j), POS(49,j), POS(81,j));
addpoints(Orbit4, POS(25,j), POS(57,j), POS(89,j));
%Plot satellites (currently only plotting lead sats due to lag)
S = zeros(1,32);
for k = 1
S(k) = scatter3(POS(k,j), POS(k+32,j), POS(k+64,j), 'Filled', 'MarkerFaceColor', [0.6 0.3 0]);
end
for k = 9
S(k) = scatter3(POS(k,j), POS(k+32,j), POS(k+64,j), 'Filled', 'MarkerFaceColor', [0 0.4 0]);
end
for k = 17
S(k) = scatter3(POS(k,j), POS(k+32,j), POS(k+64,j), 'Filled', 'MarkerFaceColor', [0.2 0.2 0.6]);
end
for k = 25
S(k) = scatter3(POS(k,j), POS(k+32,j), POS(k+64,j), 'Filled', 'MarkerFaceColor', [0.6 0 0]);
end
drawnow
%Delete satellites from current frame
delete(S)
end
0 Comments
Accepted Answer
dpb
on 27 Oct 2018
At a minimum w/o digging more deeply
1) Before beginning simulation define color vector
C=[repmat([0.6 0.3 0],8,1);
repmat([0 0.4 0],8,1);
repmat([0.2 0.2 0.6],8,1);
repmat([0.6 0 0],8,1);
2) Eliminate loops on k (coding would be easier if you had X,Y,Z coordinate arrays)
S=scatter3(POS(1:32,j),POS(33:64,j),POS(65:96,j),C,'Filled');
I suspect but have never tested that the animatedline is probably quite a bit slower than the technique of updating the [X/Y]Data values for the other portion.
2 Comments
dpb
on 27 Oct 2018
Edited: dpb
on 28 Oct 2018
Well, it seems they've rewritten the documentation a lot and it's much harder to find examples and it is claimed animatedline is fast--I suppose that is possible; I've not actually tested it myself.
The technique to which I was referring is shown in moving the marker at Marker-along-line -- one just updates the data in the object directly rather than going thru the high-level interface to do so.
You could do the same thing to build the scatter plot via line instead; all it is is in essence is an interface to line that doesn't set the line property by default.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!