简体   繁体   English

尽管使用了drawow和pause,但MATLAB GUI仍然挂断

[英]MATLAB GUI hangs up despite using drawnow and pause

I have a MATLAB GUI that looks as shown in: MATLAB GUI image 我有一个看起来像如下所示的MATLAB GUIMATLAB GUI image

What I am trying to achieve is that MATLAB keep checking for midnight continuously, except for the pauses when the user makes any change to the interface. 我想要实现的是,MATLAB会持续检查午夜,除了用户对界面进行任何更改时的停顿。 Hence, I am running a while loop in the background continuously as I need to check if it is midnight. 因此,我需要在后台连续运行一次while循环,因为我需要检查是否是午夜。 If it is, I perform some functions. 如果是,则执行一些功能。 The function that contains this while loop is called after any user input change is detected ie at the end of all the callback functions for the pop-up menus,pushbuttons, textboxes etc. This is the reason I had the drawnow in the while loop, so that if the user makes any changes and wants to run some calculations, that will be detected. 在检测到任何用户输入更改后(即,在弹出菜单,按钮,文本框等所有回调函数的末尾),都会调用包含此while循环的函数。这就是我在while循环中使用drawow的原因,这样,如果用户进行了任何更改并想要运行一些计算,就会被检测到。 After the completion of the calculations, I again call the function which has this while loop. 计算完成后,我再次调用具有此while循环的函数。

The issue is, even though I use drawnow and pause in my while loop, sometimes, not always, MATLAB still hangs-up on me and the GUI becomes unresponsive and does not recognize any of the user inputs. 问题是,即使我在窗体的循环中使用了drawow和pause,有时但并非总是如此,MATLAB仍然挂在我身上,GUI变得无响应,无法识别任何用户输入。 Here is the while loop part of my code: 这是我的代码的while循环部分:

while 1
    pause(0.1);
    drawnow;
    pause(0.1);
    current_time=clock;
    if current_time(4)==0
        post_mortem;
    end
end

I know the above code is not efficient as it will call post_mortem continuously in the midnight hour, that however is not my issue right now. 我知道上面的代码效率不高,因为它将在午夜连续调用post_mortem,但是现在这不是我的问题。 My issue is that it hangs up on me at times even at noon for example. 我的问题是,例如有时甚至在中午,它就会挂在我身上。 Does anybody have any solution to this? 有人对此有任何解决办法吗? On searching for answers to previous similar questions, the solution seemed to be to use drawnow and pause, but that doesn't seem to be working for me either. 在寻找先前类似问题的答案时,解决方案似乎是使用drawow和pause,但这似乎对我也不起作用。

Any guidance would be appreciated. 任何指导将不胜感激。

Thank you 谢谢

Since MATLAB is not multi-threaded, using a while loop to continuously check something (such as the time) is going to cause all sorts of blocking of other functionality. 由于MATLAB不是多线程的,因此使用while循环连续检查某项内容(例如时间)将导致其他功能的各种阻塞。 While drawnow and pause can potentially help with this, there are still some potential issues that can crop up. 尽管drawnowpause可能会对此有所帮助,但仍然存在一些潜在的问题。

A more elegant and reliable approach would be to use a timer object to check the time at a pre-specified interval. 一种更优雅,更可靠的方法是使用timer对象以预定的时间间隔检查时间。 This way, any user interaction with the GUI will automatically be registered and any callbacks will execute without you having to call either pause or drawnow . 这样,任何与GUI的用户交互都将自动注册,并且任何回调都将执行,而您不必调用pausedrawnow

You can create and start the timer as soon as you create your GUI. 您可以在创建GUI后立即创建并启动计时器。

% Create the timer object
handles.mytimer = timer('ExecutionMode', 'FixedRate', ...
                        'Period', 1/5, ...
                        'BusyMode', 'drop', ...
                        'TimerFcn', @(s,e)timerCallback());

% Start the timer
start(handles.mytimer)

function timerCallback()
    % Callback that executes every time the timer ticks

    current_time = clock;
    if current_time(4) == 0
        post_mortem;
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM