【问题标题】:Matlab Timed Window To Stop Code Based On User InputMatlab 定时窗口根据用户输入停止代码
【发布时间】:2014-07-03 12:23:45
【问题描述】:

我正在编写一个测试函数,它将在多个场景中运行,并且在每个场景中,我想询问用户是否愿意继续。如果他们说不,那么我将保存变量并退出程序。此函数应该有一个超时,此时代码将继续运行,并且没有选项退出,直到下一个场景开始。我的问题是超时。

我已经研究过设置 questdlg,但设置超时的唯一方法似乎是修改我无法执行的 questdlg.m 文件(由于后勤原因)。

创建消息框并使用uiwait停止代码效果很好,但是我不知道如何确定用户是否单击了确定按钮或如何使该框在超时后消失。

问题:

如何判断 msgbox 中的按钮是否被按下?

如何让消息框消失?

还有其他方法可以询问用户是否愿意停止运行测试 超时?

【问题讨论】:

    标签: matlab timeout msgbox


    【解决方案1】:

    最难的部分是你的第一个问题。

    我的第一个想法(更好的是下面)是建议您根据时间检查用户是点击 OK 还是超时:

    tic
    hmsg=msgbox('message','title','modal'); 
    uiwait(hmsg,5); %wait 5 sec
    

    然后您根据时间检查用户是否点击按钮或由于超时而继续执行:

    if toc < 5 %then the user hit the button before timeout
    %no need to close the msgbox  (user already did that)
    %appropriate code here...
    
    else %we got here due to timeout
    close(hmsg); %close the msgbox
    %appropriate code here
    end;
    

    如果他们在网络上遇到超时并且它试图关闭已经关闭的窗口,您可能会收到错误的小风险。如果这成为一个问题,我认为您可以测试句柄是否有效:

    ishandle(hmsg)
    

    在尝试关闭之前。

    这是我认为更好的方法

    hmsg=msgbox('message','title','modal');
    uiwait(hmsg,5); %wait 5 sec
    
    %now check to see if hmsg is still a handle to find out what happened
    if ishandle(hmsg) %then the window is still open (i.e. timeout)
       disp('timeout');
       close(hmsg);
       %appropriate code here...
    else %then they closed the window
       disp('user hit button');
       %other code here
    end;
    

    【讨论】:

    • 设置标签的目的是什么?此外,在 toc 之前的某处必须有一个 tic,否则会发生错误(我假设这将是 uiwait 之前的行)。
    • 抱歉 - 我已经获得了更好的解决方案的一半并且之前被卡住了。我编辑了答案,使其适用于时间方法 [tic 需要在 msgbox(....,'modal') 之前发生],但也提供了一种更好的方法来评估用户输入。干杯!
    猜你喜欢
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多