I need to break my while loop with a push of button but i get a error

i got error message for my stop button.
Reference to a cleared variable a.
Error in apptest/captureButtonPushed (line 46)
while(a==true)
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.
function stopButtonPushed(app, event)
global a;
a=0;
end
% Button pushed function: captureButton
function captureButtonPushed(app, event)
global cam;
global a;
while(a==true)
I = snapshot(cam);
Bug = vision.CascadeObjectDetector('yes.xml');
bbox = Bug(I);
detectedImg = insertObjectAnnotation(I, 'rectangle', bbox, 'cat','FontSize',40);
detectedImg = insertShape(detectedImg, 'FilledRectangle',bbox,'Opacity',0.2,'LineWidth',5);
imshow(detectedImg,'Parent',app.UIAxes2);
pause(0.01);
end
end
end

1 Commento

This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Accedi per commentare.

 Risposta accettata

Short answer: clear all; deletes a.
Longer answer:
Your immediate problem should be solved just by omitting the 'clear all'. Unless you want to specifically clear 'a' for some reason there is no point of the clear command in your function anyways. Functions have their own workspace, I cannot think of a good reason to put a 'clear all' in any function.
I'd also like to advice to not use global variables. There is usually a cleaner way; for example you could create a property a in your app and then use app.a instead of your global variable or use a toggle button and check the state of the button.

7 Commenti

if i use only clear all in the stop button i got this error message
Reference to a cleared variable a.
Error in apptest/startButtonPushed (line 26)
while a==1
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.
I dont really know how to use the other method since im dont have that deep understanding about gui matlab
Use a property to store the stop flag. That way your function will have access to it. Don't use global variables in GUIs, you don't need them. Even if you did, you should never use such a short name. You hav 63 charaters to name your global, use at least some of them to include your function name and be extremely descriptive. Can you guarantee nobody has ever used a as a global? Did you check every function you're using?
For general advice and examples for how to create a GUI have look at this thread.
im pretty sure that there was no other use of a variable in the program since i write all the program.but i just cant resolve the problem of using the global and i dont know any other method.
Attach your current code with the paperclip icon, so we can directly help you.
Also, did you search either Google or the documentation for information about how you can setup properties in AppDesigner?
this is all the file that need to run the program. i want to let the stop button in the gui to stop the while loop
Please check if this works for you.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by