While loop executes one additional loop when using appdesigner button to stop loop
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Benjamin Kraus
2024-9-23
编辑:Benjamin Kraus
2024-9-23
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 个评论
Benjamin Kraus
2024-9-24
编辑:Benjamin Kraus
2024-9-24
I did a bit more debugging and I think I know what is happening now.
- Your while loop starts. At this point cctb is 0.
- drawpoints is called.
- 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.
- While drawpoints is waiting, you click the button to stop drawing.
When you click that button, the following is happening:
- First, the drawpoints code is detecting a mouse press, so it is drawing the dot based on where you clicked.
- drawpoints finishes, and because cctb is still equal to 0, the loop repeats and drawpoints is called again.
- 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.
- You click again, and now drawpoints detects the last click and exits.
- 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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!