Matlab:检测产卵函数是否为 运行(来自周期性定时器函数)
Matlab: Detect if spawning function is running (from within periodic timer function)
我想在 MATLAB (2020) 中创建一个在控制台中显示倒计时的函数。使用例如周期性计时器,这可以很容易地实现(见下文),但是当用户在 pause(3.1)
期间按下 CTRL+C
时我无法删除计时器(并且继续执行 update_countdown
会导致混淆结果,删除控制台中的字符而不是仅显示更新时间)
由于定时器似乎 运行 是异步的,我的 想法是检测定时器生成函数 main()
是否仍然 运行ning或已被终止 - 但是我似乎无法找到一种方法来从周期性计时器函数 update_countdown
中检测到这一点。
根据我对具有
个数字的了解
- 一个可以被检测为已删除的句柄并且
- a
CloseRequestFcn()
- 一个可以指定给 运行 after a figure 已经关闭的函数,
我怀疑像这样的东西应该存在 函数。
(如何)我可以检测 main()
当前是否在我的 MATLAB 进程中 运行ning (从周期性定时器函数 update_countdown()
中)?
%save as script and run
main
function main()
timeout = 3;
t1 = timer('ExecutionMode', 'singleShot', 'StartDelay', timeout, 'TimerFcn', @finish);
t2 = timer('ExecutionMode', 'fixedRate','Period', 1, 'TimerFcn', {@update_countdown,datetime,timeout});
%^datetime is a built-in function that passes the current time
fprintf('countdown: %1i',timeout)
start(t2); start(t1); %start timers
pause(3.1); %<= arbitrary function
stop([t1 t2]); %stop timers
delete([t1 t2]); %delete timers
disp('Main: do more stuff!')
end
function update_countdown(src,ev,starttime,timeout)
%delete last displayed time, add new time (remaining)
t=round(seconds(datetime-starttime)); % time running: current time - start time
t=timeout - t;
fprintf('\b%i',t); %delete last character and replace it with currently remaining time
end
function finish(src,ev)
disp(' done!');
end
您可以使用函数onCleanup
for this. It registers a function to call when the function exits (whether normally or through Ctrl-C). See also this documentation page。
function main()
timeout = 3;
t1 = timer('ExecutionMode', 'singleShot', 'StartDelay', timeout, 'TimerFcn', @finish);
t2 = timer('ExecutionMode', 'fixedRate','Period', 1, 'TimerFcn', {@update_countdown,datetime,timeout});
%^datetime is a built-in function that passes the current time
obj = onCleanup(@() delete([t1 t2])); %delete timers when function terminates
fprintf('countdown: %1i',timeout)
start(t2); start(t1); %start timers
pause(3.1); %<= arbitrary function
stop([t1 t2]); %stop timers
disp('Main: do more stuff!')
end
我想在 MATLAB (2020) 中创建一个在控制台中显示倒计时的函数。使用例如周期性计时器,这可以很容易地实现(见下文),但是当用户在 pause(3.1)
期间按下 CTRL+C
时我无法删除计时器(并且继续执行 update_countdown
会导致混淆结果,删除控制台中的字符而不是仅显示更新时间)
由于定时器似乎 运行 是异步的,我的 想法是检测定时器生成函数 main()
是否仍然 运行ning或已被终止 - 但是我似乎无法找到一种方法来从周期性计时器函数 update_countdown
中检测到这一点。
根据我对具有
- 一个可以被检测为已删除的句柄并且
- a
CloseRequestFcn()
- 一个可以指定给 运行 after a figure 已经关闭的函数,
我怀疑像这样的东西应该存在 函数。
(如何)我可以检测 main()
当前是否在我的 MATLAB 进程中 运行ning (从周期性定时器函数 update_countdown()
中)?
%save as script and run
main
function main()
timeout = 3;
t1 = timer('ExecutionMode', 'singleShot', 'StartDelay', timeout, 'TimerFcn', @finish);
t2 = timer('ExecutionMode', 'fixedRate','Period', 1, 'TimerFcn', {@update_countdown,datetime,timeout});
%^datetime is a built-in function that passes the current time
fprintf('countdown: %1i',timeout)
start(t2); start(t1); %start timers
pause(3.1); %<= arbitrary function
stop([t1 t2]); %stop timers
delete([t1 t2]); %delete timers
disp('Main: do more stuff!')
end
function update_countdown(src,ev,starttime,timeout)
%delete last displayed time, add new time (remaining)
t=round(seconds(datetime-starttime)); % time running: current time - start time
t=timeout - t;
fprintf('\b%i',t); %delete last character and replace it with currently remaining time
end
function finish(src,ev)
disp(' done!');
end
您可以使用函数onCleanup
for this. It registers a function to call when the function exits (whether normally or through Ctrl-C). See also this documentation page。
function main()
timeout = 3;
t1 = timer('ExecutionMode', 'singleShot', 'StartDelay', timeout, 'TimerFcn', @finish);
t2 = timer('ExecutionMode', 'fixedRate','Period', 1, 'TimerFcn', {@update_countdown,datetime,timeout});
%^datetime is a built-in function that passes the current time
obj = onCleanup(@() delete([t1 t2])); %delete timers when function terminates
fprintf('countdown: %1i',timeout)
start(t2); start(t1); %start timers
pause(3.1); %<= arbitrary function
stop([t1 t2]); %stop timers
disp('Main: do more stuff!')
end