MATLAB: Uiwait Interfering with Object Wait Function in GUIDE GUI

callbackguideImage Processing Toolboxmatlab guiuiwaitwait

I have created a GUI using GUIDE to allow users to segment objects out of an image interactively using marker-controlled watershed segmentation. One of the push buttons of the GUI allows a user to draw additional regions on the image using impoly. The impoly doc says to use wait(h) to have the rest of the push button callback pause until the user double clicks the polygon, so that they have time to interactively re-size and move the object they drew. The problem I'm having is that after wait(h) is completed and the remaining push button script completes, the entire GUI closes. I think the wait function is interfering with the GUI uiwait(hObject) call in the OpeningFcn. I tried modifying the push button code so that uiwait(hObject) is called again at the end of the script, thereby preventing the GUI from closing. However, it doesn't appear to work well, as the whole GUI freezes and then MATLAB crashes.
Any ideas on how I can get the remaining push button script to wait until the user is done manipulating their object without using the wait(h) command so that it doesn't interfere with the GUI's uiwait(hObject) command? I'd really appreciate any suggestions! Thanks!

Best Answer

Try roipolyold() instead, and don't use the wait. I think that will avoid the problem. Or else try imfreehand():
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object (if needed).
binaryImage = hFH.createMask();
xy = hFH.getPosition; % Get (x,y) coordinates also.
Related Question