MATLAB: Abort roipoly in GUIDE

callbackdrawingguiguideImage Processing Toolboxroipolyslideruiwait

I created a GUI with axes that show the "slices" of a 3D matrix ( vol); a slider is used for skipping through the slices. In the slider's callback function I use roipoly to show each slice and manually segment an area from each slice.
function slider_draw_Callback(hObject, eventdata, handles)
% Get the current slice number.
slicen = round(get(hObject, 'Value'));
% Show the sliceand start drawing using roipoly.
axes(handles.axes_image);
[~, x, y] = roipoly(vol(:, :, slicen));
... some analysis with x, y...
Now, when I draw a polygon in each slice the roipoly works fine and saves x, y without issues. If I want to skip the 1st slice and draw something in the 2nd, roipoly is still "running" (crosshair visible) but can't draw anything when I click in the axes/image, giving me an eror in command window:
Error using waitfor
Error while evaluating figure WindowButtonMotionFcn
...
This is expected as roipoly needs a polygon to be drawn for it to be closed and then evoked again when I scroll to the next slice. How can I abort and restart roipoly when I press the slider buttons, essentially giving me the option to draw whenever I want? I've tried using uiwait/uiresume but can't make it work. Thanks.

Best Answer

I've only really used impoly, imroi and similar before myself. They actually return an object representing the active polygon which can be deleted, etc when required.
Even there though I tend to use a push button that I have to press if I want to draw on my image. Then only under the pushbutton does an impoly get created to draw on the image. Pressing 'Esc' can then cancel this if I change my mind.
So now having looked at roipoly, pressing 'Esc' seems to cancel this too so you probably can still do it under your slider callback rather than needing a separate button and just press Esc on a slice where you do not wish to actually draw.
Related Question