【问题标题】:Pausing Matlab Script or Function on certain user event在某些用户事件上暂停 Matlab 脚本或函数
【发布时间】:2011-06-24 19:19:23
【问题描述】:

我知道 Matlab 有可能使用 input() 暂停 Matlab 函数或脚本,因此当我有一个 for 循环并想在每次迭代时暂停它时:

for i = 1:N
  reply = input(sprintf('Pausing in iteration %d! Continue with Enter!', i), 's');

  % Calculations
end

但是如何使以下工作:通常它会在没有暂停的情况下运行(好像 input() 不存在)但是当用户执行某些操作时(可能在 matlab 中按下一个键、一个按钮或类似的东西) ,脚本/函数会暂停每次迭代,直到用户再次执行此操作,因此是这样的。例如。假设当按下某个热键组合时,matlab 有可能切换变量,例如Ctrl+Alt+A 在 0 和 1 之间切换变量 myToggle 我可以轻松做到:

for i = 1:N
  if(myToggle == 1)
    reply = input(sprintf('Pausing in iteration %d! Continue with Enter!', i), 's');
  end 

  % Calculations
end

提前致谢!

编辑:刚刚在这里找到了一种可能的解决方案:Function to ask a MATLAB program to wait for an event before continuing execution 我可以在函数/脚本的开头创建一个文件,并在下一次迭代不再存在时暂停。但这需要用户重命名不太“舒服”的文件......还有其他想法吗? :)

【问题讨论】:

标签: matlab user-controls toggle


【解决方案1】:

您可以使用带有按钮的简单 GUI;一旦按下,执行就会在下一次迭代中停止。

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

【讨论】:

  • 我正要发布一个类似的解决方案。 +1
【解决方案2】:

如果您只是想暂停脚本运行直到用户按下返回,请考虑在 MATLAB 中使用 pause 命令

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 2016-04-23
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多