Why do drawing commands, such as rectangle and line, ignore hold?

I am trying to display a changing rectangle in a loop with a pause, and it ignores the commit (which is actually assumed to be the default).

Here's a simplified version code:

clc; close all; clear all;

rect = [10 10 20 30];

figure
axis([0 200 0 50]);

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

Is it on purpose? Am I missing something? What can I do to make the previous rectangles disappear, except that they are printed in white after each iteration?

Thank..

edit:

A very simplified version has been resolved, but if I also want to draw one more thing in the same figure, the other section is ignored. What should I do in this case?

Thanks again.

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]);
    hold on
    plot (5 + 5 * i, 5, '*g');
    hold off
    pause(0.2);
end
+4
source share
3

: Matlab R2014b

Matlab R2014b . , , .


, , , , Core Graphics Objects .

:

  • , ( )

  • , ,

  • , ,

. , .

line plot - . . - , plot, . "" - , hold .

, : , .

, , 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

:

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

, :

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
+7

:

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

, .

+2

, . .

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
+1

All Articles