- A timer object, or
- A flag property in your app that you can set from the Stop button.
How can I stop my webcam loop on app designer when pressing another button?
    3 次查看(过去 30 天)
  
       显示 更早的评论
    
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? I know how to stop the vid in GUIDE but I have to use App Designer and It's not the same I think :(
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
0 个评论
回答(1 个)
  Samhitha
 2025-8-29
        In App Designer you shouldn’t rely on ishandle(figure) or global variables for stopping the webcam loop. The reason your loop doesn’t stop is because once you enter a while loop inside a callback (EmpezarButtonPushed), MATLAB blocks the event queue, so pressing the Stop button never actually executes its callback until the loop finishes. 
The correct way in App Designer is to use either: 
using a property flag: 
properties (Access = private) 
camObj        % webcam object 
isRunning     % flag to control capture loop 
end 
methods (Access = private) 
    % Button pushed function: EmpezarButton 
    function EmpezarButtonPushed(app, event) 
        app.camObj = webcam(1); 
        app.camObj.Resolution = '640x360'; 
        app.camObj.Brightness = 10; 
        app.isRunning = true; 
        % Run loop without blocking UI 
        while app.isRunning 
            img = snapshot(app.camObj); 
            imshow(img, 'Parent', app.UIAxes); 
            drawnow limitrate % allow UI updates 
        end 
    end 
    % Button pushed function: StopButton 
    function StopButtonPushed(app, event) 
        app.isRunning = false; 
        clear app.camObj; % release webcam 
    end 
end 
For more details look into the following previous MATLAB Answer: 
Hope this helps!
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

