且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在某些用户事件上暂停Matlab脚本或函数

更新时间:2023-12-05 11:28:34

您可以使用带有按钮的简单GUI;一旦按下,将在下一次迭代中暂停执行.

You can use a simple GUI with a button; once pressed the execution halts on the next iteration.

function testGUI()
    doPause = false;
    figure('menu','none', 'Position',[400 400 150 30])
    hb = uicontrol('Style','pushbutton', 'String','Pause', ...
        'Callback',@buttonCallback, 'Unit','Normalized', 'Position',[0 0 1 1]);
    %drawnow

    while ishandle(hb)
        %# check if the user wants to pause
        if doPause
            pause()             %# pauses until user press any key

            %# reset
            doPause = false;
            if ishandle(hb), set(hb, 'Enable','on'), drawnow, end
        end

        %# Calculations
        disp(rand), pause(.1)

        %# keep the GUI responsive
        drawnow
    end

    %# callback function for the button
    function buttonCallback(hObj,ev)
        doPause = true;
        set(hObj, 'Enable','off')
    end
end