Timer error: Cannot start timer because it is already running.

조회 수: 20 (최근 30일)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013년 10월 3일
편집: per isakson 2013년 10월 4일
I am trying to implement a simple timer in a GUI. It is just a figure with two buttons. I want when I press the start button to execute the updaterFcn callback function of timer. When I push the stop button I want the timer to stop. That's all. However, I get the following error (and I don't know why):
Error while evaluating StartFcn for timer 'timer-2'
Cannot start timer because it is already running.
This is my function
function stopwatch
S.fh = figure('units','pixels',...
'position',[300 300 400 400],...
'menubar','none',...
'name','stopwatch',...
'numbertitle','off');
S.pb(1) = uicontrol('style','push',...
'units','pixels',...
'position',[10 10 85 30],...
'fontsize',14,...
'string','start',...
'CallBack',@switchon);
S.pb(2) = uicontrol('style','push',...
'units','pixels',...
'position',[105 10 85 30],...
'fonts',14,...
'str','stop',...
'CallBack',@switchoff);
tmr = timer('Period',1,...
'TimerFcn',@updater,...
'StopFcn',@switchoff,...
'StartFcn',@switchon);
function updater(varargin)
disp('Timer!')
end
function switchon(varargin)
start(tmr)
end
function switchoff(varargin)
stop(tmr)
% delete(tmr)
end

채택된 답변

per isakson
per isakson 2013년 10월 3일
편집: per isakson 2013년 10월 4일
There are two problems with your timer:
  • The values of StopFcn causes stop(tmr) in switchoff to call stop a second time and correspondingly with StartFcn
  • ExecutionMode must be set to get more than one call by the timer
Try to replace your timer definition by
tmr = timer( 'Period' , 1 ...
, 'BusyMode' , 'drop' ...
, 'ExecutionMode', 'fixedDelay' ...
, 'TimerFcn' , @updater );

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by