getrect - cancel user input
Show older comments
Hi, Is it possible to cancel the getrect when the user presses (for example) escape button on the keyboard? I wrote a GUI which uses the getrect to select a rect in the image. In the case the user presses esc (when the matlab waits for rect selection, i.e. while in getrect), I want to keep the previously selected area, and return from the calculation with the new rect.
Code example:
----------------
P = getrect(handles.im1);
if (<user pressed esc event> )
return;
----------------
Is it possible to identify such event?
Thanks Hilit
1 Comment
Uday
on 26 Aug 2017
I was facing similar issue and struggled to find a solution for couple of days. Finally the following seems to work.
Replace
rect=getrect()
with
h=imrect();
rect=getPosition(h);
delete(h);
Key events will work fine now.
Answers (2)
Image Analyst
on 6 May 2016
You can use imrect() instead of getrect():
imshow('moon.tif') % Sample image.
uiwait(helpdlg('Click and drag out a rectangle.'));
title('Double-click inside rectangle to accept it, or type Esc to bail out');
someAxesHandle = gca;
% Have user draw rectangle.
hRect = imrect(someAxesHandle)
position = wait(hRect)
% Check if they typed Esc or double clicked.
if isempty(position)
uiwait(helpdlg('You typed Esc.'));
else
uiwait(helpdlg('You drew a rectangle!'));
end
fprintf('Done!\n');
This question is old, but still getting hits, so I'll try to answer it:
What blocks the ui is the following line inside the getrect function (line 106 in current 06-05-2016 code):
waitfor(GETRECT_H1, 'UserData', 'Completed');
in order to free this lock you need to do (line 251):
set(GETRECT_H1, 'UserData', 'Completed');
I had to do a toggle button with repeated getrect calls as long as it is pressed. I did something like:
function callback(do)
% i'm calling this with callback(get(toggle1,'Value))
global getrect_do
getrect_do = do;
while getrect_do
% rubberand selection.
try
% using try to catch user clicking something else, and then repeating getrect call
r = getrect(someAxesHandle);
catch
continue
end
if getrect_do
% user did mark a rectangle!
someProcess([r(1),r(1)+r(3)]);
else
% user abort. while loop will terminate
end
end
% force getrect to end
global GETRECT_H1
getrect_do = false;
set(GETRECT_H1, 'UserData', 'Completed');
Callbacks still execute even during waitfor().
Ido
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!