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

标签 matlab user-controls toggle

我知道 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我可以在我的函数/脚本的开头创建一个文件,并在它不再存在时在下一次迭代时暂停。但这将需要用户重命名不太“舒服”的文件......还有其他想法吗? :)

最佳答案

您可以使用带有按钮的简单 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

关于matlab - 在某些用户事件上暂停 Matlab 脚本或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472731/

相关文章:

c# - 处置 WPF 用户控件

matlab - 如何从 Matlab 中的元胞数组中删除最后一个元素?

matlab - 调用匿名函数,输入变量之间不使用逗号

c# - 如何从 xaml 访问 UserControl 中的按钮?

Silverlight:禁用 UI 虚拟化?

jquery - 使用子项或复合项(而不是父项)切换显示隐藏其他人?

matlab - 使用 Matlab Google-Earth Toolbox 绘制纬度和经度

algorithm - 计算matlab中大量位置成对距离的有效算法

javascript - 您如何切换链接以保持某种颜色直到再次单击

Android 不提供任何两种状态的切换按钮?