MATLAB GUI hangs up despite using drawnow and pause

17 views (last 30 days)
abc
abc on 13 Oct 2016
Commented: abc on 14 Oct 2016
Hi everyone, I have a MATLAB GUI that looks as shown below:
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. Hence, I am running a while loop in the background continuously as I need to check if it is midnight. If it is, I perform some functions. The function that contains this while loop is called after any user input change is detected i.e. in 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. After the completion of the calculations, I again call the function which has this while loop.
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. Here is the while loop part of my code:
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. 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.
Any guidance would be appreciated.
Thank you

Answers (1)

Walter Roberson
Walter Roberson on 13 Oct 2016
Edited: Walter Roberson on 13 Oct 2016
The 4th element of clock is 0 for the entire hour from midnight to 1 am. Your code is going to call the post_mortem every 0.1 second for the entire hour.
You should be considering using a timer(). Or coding
current_time = clock;
last_hour = current_time(4);
while stop_timer==0
pause(0.1); %pause triggers draw as well
current_time=clock;
current_hour = current_time(4);
if current_hour < last_hour %corrected
post_mortem;
end
last_hour = current_hour;
end
This code does not rely upon the time increment being 0.1 second: it only relies on the time increment being at most 23 hours (or perhaps 22:59.9999, I would need to think more about the exact boundary condition.). It considers midnight to have been crossed if the current hour is less than the hour at which we last checked -- so for example if your last check was 17:23 and your next check was 08:27 the next day, then it would be able to detect the midnight crossing.
  9 Comments
Walter Roberson
Walter Roberson on 14 Oct 2016
Edited: Walter Roberson on 14 Oct 2016
I do not understand why you do not use a timer set for 24 hours:
n = now;
start_delay = (ceil(n) - n) * 24*60*60;
t = timer('ExecutionMode', 'FixedRate', 'Period', 24*60*60, 'StartDelay', start_delay, 'TimerFcn', @(src, event) post_mortem);
start(t);
Just make sure that t does not go out of scope.
abc
abc on 14 Oct 2016
Thank you for your suggestion Walter. I used:
handles.mytimer = timer('ExecutionMode', 'FixedRate', ...
'Period', 15*60, ...
'BusyMode', 'drop', ...
'TimerFcn', @(s,e)checktime());
and so far haven't seen any freezing issues.

Sign in to comment.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!