How to make a timer execute an internal function in gui?

조회 수: 5 (최근 30일)
Programerck
Programerck 2017년 6월 28일
댓글: Walter Roberson 2019년 4월 19일
I am unable to call a function that is inside of GUI (not calling the external function). The timer works well if the function is external, but I want it to be internal, that I will have just one file. I have the inicialization of timer in opening function:
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.t = timer('BusyMode', 'queue', 'ExecutionMode',...
'fixedRate', 'Period', 4.0, 'TimerFcn', 'funkcija');
start(handles.t);
The function I want to run each time with usage of initialized timer:
function funkcija(EventData, handles)
a = 1;
I know there is something wrong with the input parameters, but I just cannot figure out what exactly.

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 28일
When you use a string to designate a function in a callback, then the string is eval()'d in a context equivalent to the base workspace, outside of any hierarchy. Only functions that are built-in or which have .m on the path can be accessed that way.
You should switch to using a function handle. Also, you need to make an adjustment for the fact that you are not expecting the standard parameters in your callback
handles.t = timer('BusyMode', 'queue', 'ExecutionMode',...
'fixedRate', 'Period', 4.0);
set(handles.t, 'TimerFcn', @(hObject, event) funkcija(event, handles));
Note that the handles variable that will be passed in to the callback will contain exactly whatever the handles structure contained at the time the anonymous function was created. The reason for splitting the assignment of handles.t and the setting of TimerFcn is that splitting it allows the timer handle itself to be recorded in the handles structure before that version of the structure is "frozen" for the purposes of the anonymous function.
If you want the current version of the handles object to be passed in to the function at the time the timer is fired, then the easiest route is to pass in information that allows you to figure out where the handle structure is. For example, you could use
handles.t = timer('BusyMode', 'queue', 'ExecutionMode',...
'fixedRate', 'Period', 4.0, 'TimerFcn', @funkcija);
with
function funkcija(hObject, EventData)
handles = guidata(hObject);
a = 1;
  댓글 수: 6
Programerck
Programerck 2019년 4월 19일
if you make timer as:
handles.t1 = timer('BusyMode', 'queue', 'ExecutionMode',...
'fixedRate', 'Period', 1);
set(handles.t1, 'TimerFcn', {@status1, hObject});
start(handles.t1);
guidata(hObject, handles);
than you can stop it as:
stop(handles.t1)
delete(handles.t1)
Walter Roberson
Walter Roberson 2019년 4월 19일
Also, if you give the timer a unique Tag when you create it, then instead of recording the handle to the timer, you can
stop( findtimer('Tag', 'TheTagYouGave') )

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Code Execution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by