I'm trying to display a changing rectangle in a loop, with pause, and it ignores hold off (which actually suppose to be default).
Here's a simplified version's 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, aside from ploting it in white after each iteration?
Thanks..
edit :
The very simplified version was solved, but if I want to also plot another thing on the same figure, the other plot ignores hold off. 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
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With