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

1 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-11-10
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

请先登录,再进行评论。

采纳的回答

Dennis
Dennis 2020-11-9
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by