How do I start running timer?

조회 수: 9 (최근 30일)
matar maoz
matar maoz 2011년 6월 11일
Hi
The default value for the timer's property 'running' os OFF.
When I determine its value to be 'on', like so:
T = timer('TimerFcn',@readimage,'running', 'on', 'Period', 2.0);
it gives me an error, syin that it's impossible:
*??? Error using ==> timer.timer>timer.timer at 117
Changing the 'Running' property of timer objects is not allowed.
Error in ==> timer_me at 10 T = timer('TimerFcn',@readimage,'running', 'on', 'Period', 2.0);
How do I start it?
thanks Matar

채택된 답변

Matt Fig
Matt Fig 2011년 6월 11일
Use the START function. I recommend you read the linked doc and look at other links on that page. For example, the STOP function, etc.
  댓글 수: 2
matar maoz
matar maoz 2011년 6월 11일
Thanks.
That was very helpful.
Matar
Image Analyst
Image Analyst 2012년 5월 14일
It's a little trickier than that. You have set up things correctly. See my code snippet below. It might help others even though you're all set.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 5월 14일
Below is code from a GUI that has a button to create the timer and start the timer, a timer callback function that gets executed every 0.1 second, and a button to stop the timer.
% --- Executes on button press in btnBegin.
% Start timer
function btnBegin_Callback(hObject, eventdata, handles)
% hObject handle to btnBegin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
% Get a list of all the timers that are running.
% timerObject (that I create later) will survive beyond this function,
% so if you run this function more than once,
% you will have multiple timers - they won't all be the same timer!
listOfTimers = timerfindall % List all timers.
numberOfTimers = length(listOfTimers);
% I KNOW I want ONLY ONE timer in my app.
% If there are others, (say from prior runs of this function),
% get rid of them all, so that only one remains.
if numberOfTimers > 0
delete(listOfTimers(:));
% Double check to make sure there are not any left.
listOfTimers = timerfindall % List all timers.
end
% Create a new timer object.
% The callback function that will be executed when the timer fires off
% will be called TimerCallBack() and it will take the GUIDE handles
% structure as an input argument.
fprintf('Creating timer object...\n');
timerObject = timer('TimerFcn', {@TimerCallBack, handles}, 'ExecutionMode', 'fixedSpacing', 'Period', 0.1);
% Save the timer object as a "global" variable that only our GUI can see.
setappdata(handles.figMainWindow, 'timerObj', timerObject);
fprintf('Starting timer object...\n');
% Timer does not automatically start.
% You have to manually start it with the start() function.
start(timerObject);
fprintf('Timer object should be running...\n');
catch ME
errorMessage = sprintf('Error in btnBegin_Callback().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
uiwait(msgbox(errorMessage));
end
return; % from btnBegin_Callback
% This is the callback function that gets executed whenever the timer fires off.
% There are two internal, reserved input arguments hObject and eventdata
% but we don't care about those so replace them with tildes.
% handles is the GUIDE handles structure so we can do things like
% interact with controls, set text labels, display images,
% or whatever we want to do with the user interface.
function TimerCallBack(~, ~, handles)
try
fprintf('In TimerCallBack. The time = %s\n', datestr(now));
catch ME
errorMessage = sprintf('Error in TimerCallBack().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
uiwait(msgbox(errorMessage));
end
return; % from TimerCallBack
% --- Executes on button press of btnStopTimer.
function btnStopTimer_Callback(hObject, eventdata, handles)
% hObject handle to btnStopTimer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
fprintf('Entering btnStopTimer_Callback...\n');
listOfTimers = timerfindall % List all timers, just for info.
% Get handle to the one timer that we should have.
handleToTimer = getappdata(handles.figMainWindow, 'timerObj');
% Stop that timer.
stop(handleToTimer);
fprintf('Left btnStopTimer_Callback.\n');
catch ME
errorMessage = sprintf('Error in btnStopTimer_Callback().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
uiwait(msgbox(errorMessage));
end
return; % from btnStopTimer_Callback

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by