我使用timer对象通过MATLAB 2015b 32bit的COM驱动程序对设备进行采样。 单击GUIDE按钮时,我需要停止计时器,释放设备并开始另一个序列。但我没有成功实现等待计时器停止。
建议的等待,等待,循环与暂停()和drawow的解决方案没有工作或我在某处出错。
例如:
function timer_step(~,thisEvent)
disp([datestr(now,'HH:MM:SS.FFF'), ' begin ', thisEvent.Type]);
pause(1);
disp([datestr(now,'HH:MM:SS.FFF'), ' end ', thisEvent.Type]);
end
function timer_stop(~,thisEvent)
global t;
disp([datestr(now,'HH:MM:SS.FFF'), ' begin ', thisEvent.Type]);
delete(t);
t = [];
disp([datestr(now,'HH:MM:SS.FFF'), ' end ', thisEvent.Type]);
end
function btnStart_Callback(hObject, eventdata, handles)
global t
t = timer;
t.StartFcn = @(~,thisEvent)disp([datestr(thisEvent.Data.time,'HH:MM:SS.FFF'),...
' executed ', thisEvent.Type]);
t.TimerFcn = @timer_step;
t.StopFcn = @timer_stop;
t.Period = 1;
t.TasksToExecute = 10;
t.ExecutionMode = 'fixedRate';
start(t)
function btnStop_Callback(hObject, eventdata, handles)
global t
disp([datestr(now,'HH:MM:SS.FFF') ' run stop()']);
stop(t)
disp([datestr(now,'HH:MM:SS.FFF') ' begin wait to timer finish']);
% not working solutions:
drawnow();
waitfor(t);
wait(t);
% popups new figure and blocks both this callback and timer's callback:
h = figure;
uiwait(h)
% after closing figure continues to
timeout = 5;
begin_time = tic();
while ~isempty(t) && toc(begin_time)<timeout
disp([num2str(toc(begin_time)),' waiting...']);
drawnow();
wait(t);
pause(1);
end
if ~isempty(t)
disp('got timeout!');
end
按下“开始”按钮后,在计时器停止之前输出,按“停止”按钮:
>> timer_gui
17:19:29.679 executed StartFcn
17:19:29.681 begin TimerFcn
17:19:30.692 end TimerFcn
17:19:30.694 begin TimerFcn
17:19:31.708 end TimerFcn
17:19:31.710 begin TimerFcn
17:19:31.836 run stop()
17:19:31.837 begin wait to timer finish
% here I get figure pop-up that blocks both callbacks and after closing it continues:
0.0002958 waiting...
1.0116 waiting...
2.0248 waiting...
3.0398 waiting...
4.058 waiting...
got timeout!
17:19:45.955 end TimerFcn
17:19:45.957 begin StopFcn
17:19:45.958 end StopFcn
答案 0 :(得分:0)
您只想使用stop(htimer)
停止计时器,然后使用计时器对象的StopFcn
进行所有清理。
t = timer('StopFcn', @cleanup);
function startButtonCallback(varargin)
start(t);
end
function stopButtonCallback(varargin)
stop(t);
end
function cleanup(varargin)
% Do all of your cleanup stuff here
end