Ðina YŏŜri
Ðina YŏŜri

Reputation: 55

Draw rectangles on an image within for loop

I would like to draw rectangles on an image in MATLAB. In every iteration I need to draw new rectangles and delete the previous ones.

I read the image once, and then draw the rectangles in a for loop, using

rectangle('Position',[boxPoint(1),boxPoint(2),24,32],'LineWidth',1, 'EdgeColor','g');

However, in every iteration, the rectangles are drawn on top of the old ones. Any idea how to fix this?

Upvotes: 1

Views: 751

Answers (1)

RPM
RPM

Reputation: 1769

The rectangle function can return a handle which you can subsequently use to delete it:

% Draw figure with 2 rectangles
h = figure;
hold on
xlim([0,100])
ylim([0,100])
r(1) = rectangle('Position', [10,10,10,10]); % Make sure keep handles to rectangles
r(2) = rectangle('Position', [50,50,10,10]);

delete(r(1)) % Delete a rectangle

Upvotes: 4

Related Questions