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

5 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jun 2017
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 Comments
Programerck
Programerck on 19 Apr 2019
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 on 19 Apr 2019
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') )

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!