While loop executes one additional loop when using appdesigner button to stop loop
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Benjamin Kraus
el 23 de Sept. de 2024
Editada: Benjamin Kraus
el 23 de Sept. de 2024
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 comentarios
Benjamin Kraus
el 24 de Sept. de 2024
Editada: Benjamin Kraus
el 24 de Sept. de 2024
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.
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!