1

以下のプログラムには、timer開始コマンドが続くオブジェクト定義が含まれています。その後、プログラムは他のステートメントの実行を続けます。

私の質問は、TimerFcn正確に0.01秒後に呼び出されるのか、それともタイマー コールバック関数が起動するまで for ループが完了するまで待機するのかということです。

% My timer object  
t = timer('TimerFcn',@(x,y)G2(z), 'StartDelay',0.01);
start(t);

% Other program statements 
for i=1:m
    ...
end
4

1 に答える 1

0

Bottom line is that MATLAB is effectively single-threaded. So if a long operation is currently executing, the timer callback will not get a chance to run, and according to the timer object properties (read about BusyMode), will instead add the event to a queue which MATLAB will eventually go through when it first gets a chance..

From what I understand (this is my own speculation), MATLAB timers can interrupt execution in between statements, but NOT during a lengthy one.

So in theory it should run after 0.01 second, but there are no guarantees...


The documentation says the following:

Note: The specified execution time and the actual execution of a timer can vary because timer objects work in the MATLAB single-threaded execution environment. The length of this time lag is dependent on what other processing MATLAB is performing. To force the execution of the callback functions in the event queue, include a call to the drawnow function in your code. The drawnow function flushes the event queue.

There is also this note on another doc page:

Note: Callback function execution might be delayed if the callback involves a CPU-intensive task such as updating a figure.

于 2014-07-07T11:34:35.387 に答える