Dang Khoa
Dang Khoa

Reputation: 5823

MATLAB: flushing event queue with drawnow

The function 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.

I have the following script:

clear all;
clc;

t = timer;
set(t, 'Period', 1);
set(t, 'ExecutionMode', 'fixedSpacing');
set(t, 'TimerFcn', @(event, data) disp('Timer rollover!'));

start(t);

while(1)
    %# do something interesting
    drawnow;
end

With the drawnow in place, the timer event will occur every second. Without it, no callback function occurs because the while loop is "blocking".

My questions:

1) Is there a way to flush the queue without updating figure windows?

2) When we say "flush the event queue", do we mean "execute everything in the event queue", "execute what's next in the queue and drop everything else out of the queue", or something else entirely?

I have multiple callback functions from multiple separate timers happening in the background of my program. Not having one of these callbacks executing is not an option for me. I just wanted to clarify and make sure I'm doing the right thing.

Upvotes: 0

Views: 3170

Answers (2)

Pursuit
Pursuit

Reputation: 12345

If you also place a small pause in the loop, that also frees up some time for the timer. For example pause(0.001). Some examples:

start(t); while(1);  end;              %No timer events occur
start(t); while(1); pause(0.001); end  %Timer events occur
start(t); while(1); drawnow; end       %Timer events occur (your original example)
start(t); while(1); pause(0); end      %No timer events (just thought I'd check)

Upvotes: 1

Richante
Richante

Reputation: 4388

1) Not to my knowledge - at least, I believe the only way to flush the queue is to call drawnow. Depending on what you mean by 'update figure windows', you may be able to prevent drawnow from having an undesirable effect (e.g. by removing data sources before calling drawnow).

2) I can't test this right now, but based on how I've used it before, and the description you gave above, I'm pretty sure it's "execute everything in the queue".

Another thing I'm not sure about is whether you need while 1; drawnow - don't events work as you would expect if you just end the script after start(t)? I thought drawnow was only necessary if you are doing some other stuff e.g. inside the while loop.

Upvotes: 2

Related Questions