Interactive rectangle on UIAxes for user to choose a region

I have a dataset with scattered points that I plot on a UIAxes. The idea is to let the user draw a rectangle over the UI Axes only ( not the whole figure) and select a region so that it can be processed further.
plot(app.UIAxes,app.data(1,:),app.data(2,:),'k*');
disableDefaultInteractivity(app.UIAxes);
f = app.UIFigure;
f.Units = 'normalized';
waitforbuttonpress
pos = rbbox;
annotation('rectangle',pos,'Color','r');
The code above is what I tried. The first problem that i see is that waitforbuttonpress opens a new figure and does not operate on the app figure or the axes. What should be the way to get this working?

 Accepted Answer

6 Comments

Yea it works, Thanks! However, do you think this could be done without needing the toolbox? That is the reason I tried it with rbbox.
In App Designer and apps created using the uifigure function, using uiwait and specifying a WindowButtonDownFcn or WindowKeyPressFcn callback that calls uiresume is recommended as opposed to using waitforbuttonpress because it provides more control over the app behavior. For more information, see Alternative Functionality.
Would you noob it down for me, please?
fig = uifigure('WindowButtonDownFcn',@(src,event)uiresume(src));
uiwait(fig);
I see that I can use this on a figure to click on it and make lines below uiwait work.
I am not sure how i can implement this to draw rectangle after user, for instance, clicks draw button on the app.
So I am actually able to get the start and end coordinates when i drag around the UIAxes with with following lines:
function SelectButtonPushed(app, event)
disableDefaultInteractivity(app.UIAxes)
@UIAxesButtonDown;
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
app.start = app.UIAxes.CurrentPoint;
end
% Window button up function: UIFigure
function UIFigureWindowButtonUp(app, event)
app.finish = app.UIAxes.CurrentPoint;
end
end
Not that I know very well what I am doing, I am still to make it look like the user is actually drawing a rectangle and not just dragging the pointer without knowing where it is going to end.
Still not in your line of getting the uiwait or uiresume working.
The code you are replacing has
waitforbuttonpress
pos = rbbox;
rbbox() does not offer any way to move the initial coordinates of the box: it always uses the Current Point as the starting point. The waitforbuttonpress is there to give the user an opportunity to move the cursor to where they want to start the box, and then click, after which rbbox can then work.
When using App Designer, waitforbuttonpress() is not recommended. Instead, you would set the WindowButtonDownFcn callback for the uifigure that contains the area you want to draw into. The uiresume() callback you have should be fine.
However... rbbox and other similar calls outside of Image Processing Toolbox only work on traditional figures, and all of the recommendations that I can find for how to proceed lead back to drawrectangle() .
In order to proceed without the toolbox, you will need to program your own box dragging function. You might find it useful to look at https://www.mathworks.com/matlabcentral/answers/458264-using-windowbuttondownfcn-in-app-designer#comment_2132605
I just successfully tested this code:
fig.WindowButtonDownFcn = @(src,event)uiresume(src);
uiwait(fig);
out = uirbbox(fig);
disp(out)
Where uirbbox is a modified version of Mathwork's rrbox that expects a uifigure as the first parameter.
function finalRect = uirbbox(f, initialRectangle, anchorPoint, step)
and
arguments
f (1, 1) matlab.ui.Figure
and
for the chain of if nargin == add 1 to each of the counts, so like
if nargin == 1
initRect = [];
fixedPoint = [];
stepSize = [];
Then comment out
f = gcf;
With f now being a parameter that is a uifigure, and with the gcf commented out, and with the argument count incremented by 1 as appropriate... then it works.
... It does, however, take remarkably long to activate.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!