Clear Filters
Clear Filters

While loop executes one additional loop when using appdesigner button to stop loop

47 views (last 30 days)
Hi All,
I am trying to build a app to count the number of circles in a image manually using the drawpoint and display the count. The code is working with the drawpoint and displaying the count but when i try to stop the loop with the stop button (break command) is executes one more drawpoin and circle count. So i get a unwanted additional point on the image and additional count in the display.
Code:
% Button pushed function: StartCountTslotButton
function StartCountTslotButtonPushed(app, event)
app.cctb = 0;
app.n = 0;
app.EditField3.Value = app.cctb;
while app.cctb==0
app.n=app.n+1;
app.h = drawpoint("Parent",app.image);
app.TSlotCond.Value = app.n;
if app.cctb==1
break
end
end
end
% Button pushed function: CompTslotButton
function CompTslotButtonPushed(app, event)
app.cctb=1;
app.EditField3.Value = app.cctb

Answers (1)

Benjamin Kraus
Benjamin Kraus on 23 Sep 2024 at 23:19
Edited: Benjamin Kraus on 23 Sep 2024 at 23:29
Callbacks (like StartCountTslotButtonPushed and CompTslotButtonPushed) will only run if you tell MATLAB to process pending callbacks by calling drawnow.
drawpoint is internally calling uiwait, which is having the same effect as drawnow would have (it is processing any pending button presses), but that is happening too late to recognize that you've pressed the button.
If you add a call to drawnow before you check the value of cctb, it should fix this issue.
app.h = drawpoint("Parent",app.image);
app.TSlotCond.Value = app.n;
drawnow % Process any pending button pushes
if app.cctb==1
break
end
  2 Comments
Adrian
Adrian on 24 Sep 2024 at 8:22
Thank you for your response, I have added the drawnow into the code and it now breaks out the while loop. It still excutes the while loop on the press of the "CompTslotButtonPushed" and therefore i get an additionl point of the image and counter?
Benjamin Kraus
Benjamin Kraus on 24 Sep 2024 at 13:09
Edited: Benjamin Kraus on 24 Sep 2024 at 13:11
I did a bit more debugging and I think I know what is happening now.
  1. Your while loop starts. At this point cctb is 0.
  2. drawpoints is called.
  3. Within drawpoints it is adding a listener for either the escape key or a mouse button press. It is going to wait continuously until one of those two events occurs.
  4. While drawpoints is waiting, you click the button to stop drawing.
When you click that button, the following is happening:
  1. First, the drawpoints code is detecting a mouse press, so it is drawing the dot based on where you clicked.
  2. drawpoints finishes, and because cctb is still equal to 0, the loop repeats and drawpoints is called again.
  3. Once drawpoints is called again, it gives an opportunity for the button click to be processed, which sets cctb to 1, but drawpoints is still waiting and listening for a mouse press.
  4. You click again, and now drawpoints detects the last click and exits.
  5. Now cctb is equal to 1, so the loop ends.
Adding a drawnow after drawpoints modifies step 2: When drawpoints finishes, the drawnow causes the button click to be processed immediately, setting cctbto 1 so the loop exists. But, drawpoints still detected the mouse click, so you still got that last dot.
You could use the Escape key to abort the call to drawpoints, but that doesn't set cctb so it is just called again, so that alone isn't sufficient.
A sort of simple fix would be to delete the last point drawn by drawpoints. Something like this:
app.h = drawpoint("Parent",app.image);
app.TSlotCond.Value = app.n;
drawnow % Process any pending button pushes
if app.cctb==1
delete(app.h) % Delete the last point drawn
break
end
This causes a point to show up and then disappear, but it is a quick and easy solution.
If that doesn't work, then you will probably have to "roll your own" drawpoints using things like waitfor, ButtonDownFcn, and CurrentPoint.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!