MATLAB drawrectangle ROI custom wait function difficulty

Hi,
In an app I am creating, the user loads in an image then will have to select a square section using drawrectangle. I want to allow the user to move the ROI around for a bit rather than just having it execute the moment the mouse is released, so I have tried using this custom wait function:
I am having trouble progressing in the code though. Here is what I am working with inside my GUI:
figure(1) % create pop out window
imshow(app.I) % show the image
sqSect = drawrectangle; % create rectangle section
[AR,app.pos] = customWait(app, sqSect); % i want the aspect ratio and the position to be returned.
% the aspect ratio is only needed within the function, so I don't include it in the app object
close(h) % after getting the section, I want the pop out window to close
%%*******more stuff here using the app.pos data, not relevant to this issue*******
function [AR,pos] = customWait(~,hROI)
% Listen for mouse clicks on the ROI
l = addlistener(hROI,'ROIClicked',@clickCallback);
% Block program execution
uiwait;
% Remove listener
delete(l);
% Return the current position
pos = hROI.Position;
AR = hROI.AspectRatio;
end
function clickCallback(~,evt)
if strcmp(evt.SelectionType,'double')
uiresume;
end
end
When I run this in my GUI, I can create the rectangle section, but then when I double click it, the pop out window does not close... How do I adjust this to make the pop out window close when I double click the ROI (signifying that I have chosen the right section and am ready to progress to the next stuff)???
EDIT:
the following works in a script, but not in an App for some reason...?
imshow('DSC_4111.png')
h = drawrectangle
pos = customWait(h)
function pos = customWait(hROI)
% Listen for mouse clicks on the ROI
l = addlistener(hROI,'ROIClicked',@clickCallback);
% Block program execution
uiwait;
% Remove listener
delete(l);
% Return the current position
pos = hROI.Position;
end
function clickCallback(~,evt)
if strcmp(evt.SelectionType,'double')
uiresume;
end
end
When I put that in app form, something goes wrong and it gives me that error:
Undefined function 'clickCallback' for input arguments of type 'images.roi.Rectangle'.
Warning: Error occurred while executing the listener callback for event ROIClicked defined for class
images.roi.Rectangle:
Somehow I'm getting an error about the inputs for the clickCallback function even though it works in the exact same format in the app form.. Maybe something to do with the object oriented environment of an app?

3 Comments

I ran into this a few months ago and got the quirky solution from MATLAB. I'll try to dig it up for you tomorrow. It's kind of tricky. (You knew it had to be if I had to call tech support. Even the tech support had to call the developers!! So it's not obvious.)
Could you figure it out ? I am having same issue :/

Sign in to comment.

 Accepted Answer

Store the figure in h so that when you later call close(h), MATLAB knows what h is:
h = figure(1);

4 Comments

Didn't work :(
This is the error I'm getting:
Undefined function 'clickCallback' for input arguments of type 'images.roi.Rectangle'.
Warning: Error occurred while executing the listener callback for event ROIClicked defined for class
images.roi.Rectangle:
Something to do with the clickCallback function is not working. Can someone verify whether I'm using correct input arguments?
Tommy,
I think it may be related to what you were talking about before on this post:
based on the error I am getting, it seems I may need to change up the input arguments.. I'm new to obj oriented stuff though so very confused.
Ah okay, it seems like after you moved these functions into your app, MATLAB was unable to find clickCallback from within customWait. Where did you put these two functions? It seems like MATLAB can find customWait, based on the error. Does it work if you put clickCallback within customWait? i.e.
function pos = customWait(hROI)
% Listen for mouse clicks on the ROI
l = addlistener(hROI,'ROIClicked',@clickCallback);
% Block program execution
uiwait;
% Remove listener
delete(l);
% Return the current position
pos = hROI.Position;
function clickCallback(~,evt)
if strcmp(evt.SelectionType,'double')
uiresume;
end
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!