필터 지우기
필터 지우기

How to Create RT clock in my own GUI?

조회 수: 2 (최근 30일)
Mark Golberg
Mark Golberg 2017년 6월 26일
댓글: Mark Golberg 2017년 6월 27일
Hello,
I have the following code (it's upon GUI creation):
set(handles.time_caption,'String',datestr(now,13))
tmr = timer('Name','Reminder', ...
'Period',1, ... % Update the time every 1 seconds.
'StartDelay',0, ... % In seconds.
'TasksToExecute',inf, ... % number of times to update
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@updater_mg});
start(tmr); % Start the timer object.
function [] = updater_mg(varargin)
set(handles.time_caption , 'string' , datestr(now,13))
end
I receive the following error:
Error while evaluating TimerFcn for timer 'Reminder'
Undefined variable "handles" or class "handles.time_caption".
I understand that when I'm "inside" the updater_mg function it doesn't recognize my handles struct from a layer above.
How can I overcome this?

채택된 답변

Jan
Jan 2017년 6월 26일
편집: Jan 2017년 6월 26일
If handles is unknown in the callback, provide it as input:
set(handles.time_caption,'String',datestr(now,13))
tmr = timer('Name','Reminder', ...
'Period',1, ... % Update the time every 1 seconds.
'StartDelay',0, ... % In seconds.
'TasksToExecute',inf, ... % number of times to update
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@updater_mg, handles}); % <== HERE
function updater_mg(TimerH, EventData, handles) % <== AND HERE
set(handles.time_caption , 'string' , datestr(now,13));
end
Add a security check:
function updater_mg(TimerH, EventData, handles) % <== AND HERE
if ishandle(handles.time_caption)
set(handles.time_caption , 'string' , datestr(now,13));
else
disp('Timer for clock stopped: GUI was closed');
stop(TimerH);
end
end
  댓글 수: 4
Jan
Jan 2017년 6월 27일
Search in the documentation of Matlab for the term "callback":
docsearch callback
There you find the description, that all callbacks get two default inputs: The handle of the affected object and a description of the event. Therefore definining the callback with 1 output means, that it is called with 3 inputs. For 'TimerFcn', @updater_mg the callback would get 2 inputs.
Mark Golberg
Mark Golberg 2017년 6월 27일
Great, Thanks again.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by