Hello,
I am hoping that someone will help me with a problem I am having. I am trying to make a for loop in order to remove a number of given rectangles. However, I have notice that when I run the code and debug it, the deletion is not shown on the graph. I am not sure how to approach this. Even if this may be a simple problem, I would appreciate any input on this situation.
The Code:
axis([0 20 0 5])
rectangle('Position',[0,1,1,1])
rectangle('Position',[1,1,1,1])
rectangle('Position',[2,1,1,1])
rectangle('Position',[3,1,1,1])
rectangle('Position',[4,1,1,1])
rectangle('Position',[5,1,1,1])
rectangle('Position',[6,1,1,1])
rectangle('Position',[7,1,1,1])
rectangle('Position',[8,1,1,1])
rectangle('Position',[9,1,1,1])
rectangle('Position',[10,1,1,1])
rectangle('Position',[11,1,1,1])
rectangle('Position',[12,1,1,1])
rectangle('Position',[13,1,1,1])
rectangle('Position',[14,1,1,1])
rectangle('Position',[15,1,1,1])
x=15;
for i=1:10
h=rectangle('Position',[x,1,1,1])
H=findobj(h)
delete(h)
x=x-1;
M(i)=getframe
end
movie(M)
Thank you in advance.

 채택된 답변

Stephen23
Stephen23 2016년 7월 18일
편집: Stephen23 2016년 7월 18일

1 개 추천

You are drawing every rectangle twice: once hardcoded before the loop, and once inside the loop. This means deleting one of them has no effect because the other is still visible. Try this:
axis([0,20,0,5])
V = 1:15;
for k = V
hnd = rectangle('Position',[k,1,1,1]);
M(k)=getframe;
delete(hnd)
end
movie(M)

댓글 수: 5

Salvador Montes
Salvador Montes 2016년 7월 18일
Hello,
I have just tried drawnow, but the graph still does not update. Is there more that I may be able to do?
Image Analyst
Image Analyst 2016년 7월 18일
What are your units? If they're normalized, then the rectangle will be way off the right side of the image and you won't see it. If they are pixels then you should see it.
Make sure your drawnow is before you call delete. You might also call pause(0.2) to give you time to see it before it gets deleted.
Salvador Montes
Salvador Montes 2016년 7월 18일
I did not realized that I had two rectangles. This works much better than what I previously had. Thank you very much for your help and for noticing my mistake.
Thanks again, and have a great day
Salvador Montes
Salvador Montes 2016년 7월 18일
편집: Salvador Montes 2016년 7월 18일
Hello Image Analyst,
I have tried checking my units on the rectangle but unfortunately I get the error message that there are no units for the rectangle class. I am not sure if this is common when using 'rectangle'.
Allow me then to ask you what is your suggestion on this situation?
Image Analyst
Image Analyst 2016년 7월 18일
No, it would be the 'units' property of the axes that you're drawing the rectangles on.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2016년 7월 18일

댓글:

2016년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by