MATLAB GUI hangs up despite using drawnow and pause
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi everyone, I have a MATLAB GUI that looks as shown below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/157135/image.png)
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
0 comentarios
Respuestas (1)
Walter Roberson
el 13 de Oct. de 2016
Editada: Walter Roberson
el 13 de Oct. de 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 comentarios
Walter Roberson
el 14 de Oct. de 2016
Editada: Walter Roberson
el 14 de Oct. de 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.
Ver también
Categorías
Más información sobre Graphics Objects en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!