简体   繁体   English

如何在 App Designer 中停止我的网络摄像头循环? Matlab

[英]How can I stop my webcam loop in App Designer? Matlab

I'm doing an interface in app designer where I turn on my webcam when I press a button(Empezar button), the only problem is that I can't find a way on how to stop my webcam, It's on a loop and I need to finish that loop when I press another button (stop button), but I've tried a lot of things and nothing really seems to work, I tried with a buttonpressfcn didn't work, opening a figure and when it closes the figure the vid stops didn't work either, any ideas?我在应用程序设计器中做一个界面,当我按下一个按钮(Empezar 按钮)时我打开我的网络摄像头,唯一的问题是我找不到如何停止我的网络摄像头的方法,它在一个循环中,我当我按下另一个按钮(停止按钮)时需要完成该循环,但我已经尝试了很多东西,但似乎没有任何效果,我尝试使用 buttonpressfcn 不起作用,打开一个图,当它关闭图视频停止也不起作用,有什么想法吗? I know how to stop the vid in GUIDE but I have to use App Designer and It's not the same I think:(我知道如何在 GUIDE 中停止 vid,但我必须使用 App Designer,而且我认为这不一样:(

    properties (Access = public)
        UIFigure       matlab.ui.Figure
        StopButton     matlab.ui.control.Button
        EmpezarButton  matlab.ui.control.Button
        UIAxes         matlab.ui.control.UIAxes
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: EmpezarButton
        function EmpezarButtonPushed(app, event)
%         clear all;
            
        
            
            micamara=webcam(1);
            micamara.Resolution='640x360';
            micamara.Brightness=10;
            %ventana=app.StopButton.ButtonPushedFcn;
            %while ishandle(ventana)
            ventana=figure;
             while ishandle(ventana)
               img=snapshot(micamara);
               imshow(img,'Parent',app.UIAxes);
               
                
            end
            
        end

        % Button pushed function: StopButton
        function StopButtonPushed(app, event)
            global ventana;
             ventana=1;
        end
    end

Hey engineerinprocess (aren't we all..?)嘿,engineerinprocess(我们不都是..吗?)

The infinite loop in your EmpezarButtonPushed callback makes it impossible for Matlab to handle other callbacks, like your StopButtonPushed callback. EmpezarButtonPushed 回调中的无限循环使 Matlab 无法处理其他回调,例如 StopButtonPushed 回调。 That's way it does not work.这样就行不通了。

The proper way of solving this would be using a timer.解决这个问题的正确方法是使用计时器。

  • The setup-code I would move to the 'startupFcn' of your app, and be sure to store the micamera as a property of your app.我会将设置代码移至您的应用程序的“startupFcn”,并确保将云母存储为您的应用程序的属性。 You'll need it later.你稍后会需要它。 In the startupFcn you also initialize a timer with a certain period.在 startupFcn 中,您还初始化了一个具有一定周期的计时器。
  • A timerFcn callback can then do the things you have in your while loop.然后 timerFcn 回调可以执行您在 while 循环中所做的事情。
  • The EmpazarButtonPushed now starts the timer (if not already running) EmpazarButtonPushed 现在启动计时器(如果尚未运行)
  • The StopButtonPushed callback should stop the timer (if running). StopButtonPushed 回调应该停止计时器(如果正在运行)。

You should also add a CloseRequestFcn callback for your figure, where you stop and delete your timer object.您还应该为您的图形添加一个 CloseRequestFcn 回调,您可以在其中停止并删除您的计时器 object。 Another thing I usually do during development, EngineerInProcess, is to put the contents of the timer callback in a try/catch block, where in the catch block I display the error message and stop the timer.我在开发过程中通常会做的另一件事 EngineerInProcess 是将计时器回调的内容放在 try/catch 块中,在 catch 块中我显示错误消息并停止计时器。

There is actually an example in the appdesigner on how to use timers in apps.在应用程序设计器中实际上有一个关于如何在应用程序中使用计时器的示例。 You may want to check that out as well.您可能也想检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM