1
votes

I have a question about matlab timer object. I have a button in my gui that creates a timer and every period of 0.1 i want to call my function.

function playBtn_Callback(hObject, eventdata, handles)
global pl;
global c;
global g;
global t;
global myData;
if isplaying(pl)
     pause(pl);
     set(handles.playBtn, 'string', 'Play');
else 
     resume(pl);
     set(handles.playBtn, 'string', 'Pause');
end
myData=guidata(handles.timertext);
c=get(pl, 'CurrentSample')
g=get(pl, 'TotalSample')
a = timer;
set(a, 'ExecutionMode', 'FixedRate');
set(a, 'TimerFcn', 'myFunction', 'Period', 0.1);
start(a);



function t = myFunction()
global mydata;
global pl;
global sf;
global mySong;

c=get(pl, 'CurrentSample')
set(mydata.timertext, 'String', c/sf);

sf is the frequency of my song.

The error i get is this:

Error while evaluating TimerFcn for timer 'timer-26'

Undefined function or variable 'myFunction'.

1

1 Answers

-1
votes

You need to pass a function handle for the value of 'TimerFcn', not a string. Because your function is within your UI m-file, it cannot be accessed externally (without some trickery), so you have to pass an actual function handle rather than a string. (otherwise it will call str2func and get a function handle it can't use).

If you don't know what I'm talking about, read http://www.mathworks.co.uk/help/matlab/matlab_prog/creating-a-function-handle.html .

You should also look at the documentation for the guidata function - there is no need to use global variables.