Increasing speed by fixing axis and grid outside of a loop

Heres a minimal example of what I mean: Axis and grid are set before the loop
Pos = [0 0];
axis([0 1 0 1]);
grid on
for i = 1:20
Pos = [0 0] + i;
plot(Pos(1), Pos(2),'x')
%axis([0 1 0 1]);
%grid on
pause(0.1)
end
However this will not fix the axis or the grid. Instead you have to define it inside the loop.For this minimal example it shouldn't matter, but believe it or not for larger projects I figured the program slacks a little when axis and grid are defined in the loop so how to really fix it outside?

2 Comments

Please explain how your plot should look after finishing.
Excuse me for the delayed answerte I attempted to give more detail on what I seek doing below.

Sign in to comment.

 Accepted Answer

I think what Torsten is getting after is: do you intend to keep the history of all the previous pairs you plotted?
But I'm going to assume you don't, and you just want to replace the plot series while keeping the other axes properties fixed, while many/most/all of them will auto-reset with the default axes property "NextPlot" value set to "replace"
One way to do it
ax = axes("XLim",[0,21],"YLim",[0,21],...
"XGrid","on","YGrid","on",...
"NextPlot","add");
% you need NextPlot to be "add" so that the first plot you make doesn't
% undo your axes properties set in the definition, which, now that I think
% about it, is a little weird.
% return the lineseries object from plot so you can reference and alter it
% later
ph = plot(nan,nan,"x");
pos = [0,0];
for i = 1:20
pos = pos + 1;
ph.XData = pos(1); % set the XData property of the lineseries object
ph.YData = pos(2); % set the YData property
drawnow
end

4 Comments

Thank you for your reply. However this won't plot any visbile points at least I don't see some?
Instead of publishing my full code (it's quiete long) I explain what I'm coding:
N-Body-Simulation: You have got multiple points spread in 2D, each interacting due to the gravitational force.
This causes all Points to change position while the loop is running.
Here as I descibed the axis are changing in the loop even if specified them outside : (as demonstrated in the minimal example)
So what I which to do is plotting multiple points for each iteration in the loop on a 2D-plot that has fixed axis.
But: axis shouldn't be specified in the loop, since I empirically noticed his will slow down the speed.
If I repeated myslef and my desire remains still unclear to you, please enquiere as to more detail.
you dont see any points because i just copy pasted your minimal example - the last point is (20,20) outside your axis limits to [0,1,0,1]; i guess you wouldn't even have seen your first point as it would be on the very corner.
The good news is, this should already demonstrate to you that you can set the axes limits outside the loop, the key being to use "NextPlot" "repace" and to use a plot handle whose data you update, instead of replacing the plot every iteration.
Oh yea absolutely, I also had an ambiguity error in my main code, that's why I was taken aback. Now it works like a charm after a critical look. Thank you for you assistance!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!