Why do drawing commands like 'rectangle' and 'line' ignore 'hold off'?

余生长醉 提交于 2019-12-01 20:44:06

Update: Matlab R2014b and later

With the update of the handle graphics engine in Matlab R2014b the notation in the documentation changed a little. There are no distinctive sites for Core Graphics Objects and Plot Objects anymore, but here you can see that the two kind of objects are now called Plotting Function Objects and Primitive Objects.


The reason is, that there is a difference between Core Graphics Objects made for drawing and its subgroup Plot Objects with the purpose of displaying data, using Core Graphics Objects to do that.

Core Graphics Objects

Core graphics objects include basic drawing primitives:

  • Line, text, and polygon shells (patch objects)

  • Specialized objects like surfaces, which are composed of a rectangular grid of vertices

  • Images

  • Light objects, which are not visible but affect the way some objects are colored

Plot Objects

A number of high-level plotting functions create plot objects. The properties of plot objects provide easy access to the important properties of the core graphics objects that the plot objects contain.

A characteristic example would be the commands line and plot - which are basically the same. But they belong to different groups. If one wants to plot data, he uses plot, task done. If one wants to "draw" something with lines its easier you wouldn't always need to hold everything.

So to answer your question: Yes, it is on purpose.

And to solve your problem, I would write a new rectangle function using plot:

function h = plotRectangle(posX, posY, width, height)

x = [posX posX+width posX+width  posX        posX];
y = [posY posY       posY+height posY+height posY];

h = plot(x,y);

end

Respectively:

function h = plotRectangle(PosVector)

X = PosVector;

x = [X(1) X(1)+X(3) X(1)+X(3)  X(1)        X(1)];
y = [X(2) X(2)      X(2)+X(4)  X(2)+X(4)   X(2)];

h = plot(x,y);

end

The latter you can now use with your code:

rect = [10 10 20 30];

figure
axis([0 200 0 50]);

for i = 1 : 15
    rect(1) = rect(1) + i;
    plotRectangle(rect);
    hold off;
    pause(0.2);
end

Modified your code:

clc; close all; clear all;

rect = [10 10 20 30];

figure
axis([0 200 0 50]);

h1 = [];
for i = 1 : 15
    rect(1) = rect(1) + i;
    delete(h1);
    h1 = rectangle('Position', rect, 'edgeColor', [1 0 0]);
    pause(0.2);
end

Hope this helps..

You can use the same method that lakesh already proposed. Below I added an additional handle for the second plot.

clc; close all; clear all;

rect = [10 10 20 30];

figure; hold on;
axis([0 200 0 50]);

h1 = [];
h2 = [];
for i = 1 : 15
    rect(1) = rect(1) + i;
    delete(h1);
    delete(h2);
    h1 = rectangle('Position', rect, 'edgeColor', [1 0 0]);
    h2 = plot (5 + 5 * i, 5, '*g');

    pause(0.2);
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!