由前一篇文章可以看出,按照所设定的时间间隔调用Timer的回调函数时,并不是真的即刻执行该回调函数,而是先将该回调函数放入事件队列中,待前面的事件都执行完时才执行该事件。但有时我们需要当所设定的时间到达时立刻执行我们的回调函数,此时我们就可以用功能强大的drawnow函数了。在回调函数中加入drawnow就可以在时间到达时强制执行该回调函数。
下面给出的是matlab帮助里关于drawnow的用法:
drawnow
Flush event queue and update figure window
Syntax
drawnow
drawnow expose
drawnow update
Description
drawnow causes figure windows and their children to update and flushes the system event queue. Any callbacks generated by incoming events (e.g. mouse or key events) are dispatched before drawnow returns.
drawnow expose causes only graphics objects to refresh, if needed. It does not allow callbacks to execute and does not process other events in the queue.
drawnow update causes only non-graphics objects to refresh, if needed. It does not allow callbacks to execute and does not process other events in the queue.
You can combine the expose and update options to obtain both effects.
drawnow expose update
Other Events That Cause Event Queue Processing
Other events that cause MATLAB to flush the event queue and draw the figure includes:
*
Returning to the MATLAB prompt
*
Executing the following functions
o
figure
o
getframe
o
input
o
keyboard
o
pause
*
Functions that wait for user input (i.e., waitforbuttonpress, waitfor, ginput)
*
Any code that causes one of the above functions to be executed. For example, suppose h is the handle of an axes. Calling axes(h) causes its parent figure to be made the current figure and brought to the front of all displayed figures, which results in the event queue being flushed.
Examples
Using drawnow in a loop causes the display to update while the loop executes:
t = 0:pi/20:2*pi;
y = exp(sin(t));
h = plot(t,y,'YDataSource','y');
for k = 1:.1:10
y = exp(sin(t.*k));
refreshdata(h,'caller') % Evaluate y in the function workspace
drawnow; pause(.1)
end