Increasing speed by fixing axis and grid outside of a loop

조회 수: 1 (최근 30일)
Niklas Kurz
Niklas Kurz 2022년 10월 31일
댓글: J. Alex Lee 2022년 11월 2일
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
Torsten
Torsten 2022년 10월 31일
Please explain how your plot should look after finishing.
Niklas Kurz
Niklas Kurz 2022년 11월 2일
Excuse me for the delayed answerte I attempted to give more detail on what I seek doing below.

댓글을 달려면 로그인하십시오.

채택된 답변

J. Alex Lee
J. Alex Lee 2022년 11월 1일
편집: J. Alex Lee 2022년 11월 2일
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
Niklas Kurz
Niklas Kurz 2022년 11월 2일
편집: Niklas Kurz 2022년 11월 2일
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!
J. Alex Lee
J. Alex Lee 2022년 11월 2일
awesome, glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by