One way is to wrap the position acquisition in a while loop and add some sort of uicontrol
to indicate when you're done. In this case, it's just a pushbutton that deletes the imrect
handle when you're done. If the handle is deleted, h
is no longer valid, so isvalid
stops the while
loop. ~isempty
is used as the last returned value of p
will be empty.
Note that this code overwrites p
each time so if you want to record all the positions, not just draw rectangles, you will need to add that in.
figure
imshow(I)
h = imrect
uicontrol('Style', 'pushbutton', 'String', 'Done',...
'Position', [20 20 50 20],...
'Callback', 'delete(h)');
while isvalid(h)
p = wait(h);
if ~isempty(p)
rectangle('Position', p, 'LineWidth',2, 'EdgeColor','r');
end
end