Draw points on UIAxes until you press a button in APP DESIGNER

Hi, I have a problem, I'm developing an application using the app designer but there comes a moment when I have to be able to select points from an image I show in a UIAxes using the drawcircle function.
What I need is to be able to enter points (without having defined how many in advance) until I press on a status button, but I also need to save the position of the points, what I came up with was this but it doesn't work well
i = 1;
ROI = zeros(8,2);
while app.AplicarInterpolacinButton.Value == 0
a = drawpoint(app.UIAxes);
ROI(i,:) = a.Position;
i = i + 1;
end
Thank you.

5 Comments

I put it before, the question is how can I enter points with the drawpoint function in an image I show in a UIAxes and how do I store the position of them until I press a button, because with the code I show before it doesn't work
Your code should already be doing that. When you call drawpoint(app.UIAxes), it starts an interactive draggable point within your axes and it waits for the user to interact with it. It may require r2020a. To get the (x,y) coordinate of the point, a.Position.
Yes but when I press the button it doenst stop, i have to enter another point... and i'm usiing r2020a
Now the problem is clear. I'll write an answer below.

Sign in to comment.

 Accepted Answer

Once drawpoint() is called, it waits for a response and the waiting time cannot be interrupted until you either draw a point, press escape, or close the figure. Since the rest of your while-loop is very fast, there is very little time between iterations so there is very little time that your code can detect that the stop-button was pressed. Therefore, it's very highly likely that any randomly timed button press will occur during the drawpoint() execution and the button press will not be detected until either a point is drawn or the user presses escape or the figure is closed. There's no way to programmatically end the drawpoint() wait-time.
If you can figure out an algorithm that detects when enough points were drawn, you could end the while-loop prior to initializing the next drawpoint.
To end loop when escape is pressed or figure is closed
When you end drawpoint by pressing escape, it returns an empty position value. When you end drawpoint by closing the figure, a "handle to deleted point" is returned.
To continually draw points until esc is pressed or the fig is closed, just test of those conditions.
userStopped = false;
pointhandles = gobjects();
while ~userStopped
a = drawpoint();
if ~isvalid(a) || isempty(a.Position)
% End the loop
userStopped = true;
else
% store point object handle
pointhandles(end+1) = a;
end
end

10 Comments

Ok I understand, thank you so much, i think that i will create a new figure and until I close i could introduce the number of points I want, thank you!
That's a fine idea.
A second idea is to detect if the user drew a final point after the button was pressed and to delete it. But I like your idea better.
But i also have a problem with the idea of put the drawpoint into another figure:
f = figure;
imshow(app.I);
i = 1;
while app.AplicarInterpolacinButton.Value == 0
a = drawpoint(app.UIAxes);
ROI(i,:) = a.Position;
i = i + 1;
end
If I make something like this when I close the figure it appears an error in the comand windows, how can I fix it? I mean, how can I keep the mistake from being seen?
What's the full error message? In general, whenever you mention an error message you should provide the entire message.
Also, I don't see where you're using the new figure. I see that you create it but the drawpoint() command is still assigned to your app's UIAxes.
It may be simpler to just press escape when you're done rather than producing the points on an eternal figure and closing the figure.
Well, forget the mistake, I still don't really know how to do it, I mean:
I need to program it in such a way that the user can enter as many points as he wants, either until he presses ESC or until he closes the figure and saves the position of all the points in a matrix, but, what condition do I put in the while to let me enter points either until I press esc or until I close the figure?
The correct code for the previus was this:
f = figure;
imshow(app.I);
i = 1;
while [What shuld I put Here]
a = drawpoint;
ROI(i,:) = a.Position;
i = i + 1;
end
Ok, that's an easier task.
I'll update my answer.
Ok, thank you so much for all again!!!
I am trying to run the above code in a GUI and mark points over an image. I can not move or delete the points as clicking with both mouse buttons generate more points, can it be fixed? Is it possible to mark the points with text? and Furthermre, after pressing the ESC, how can I get the coordinates?
function StartButtonPushed(app, event)
imshow('peppers.png','Parent',app.ImageAxes);
userStopped = false;
pointhandles = gobjects();
while ~userStopped
a = drawpoint(app.ImageAxes);
if ~isvalid(a) || isempty(a.Position)
% End the loop
userStopped = true;
else
% store point object handle
pointhandles(end+1) = a;
end
end
disp(pointhandles)
end
For everything else:
function draw
idx = 0;
positions = [];
fig = figure(1);
ax = axes;
userStopped = false;
btn = uicontrol('Parent',gcf,'Style','pushbutton','string','Undo!', 'Callback', @undo);
while ~userStopped
a = drawpoint();
idx = idx + 1;
if ~isvalid(a) || isempty(a.Position)
userStopped = true;
else
positions(:,:,idx) = a.Position;
assignin("base", "positions", positions)
end
end
end
function undo(src, event)
ud = findobj('Type','images.roi.Point');
delete(ud);
draw
end

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!