Search code examples
matlabtimercallbackhandle

Matlab Timer function to start another function


I want to use the timer function to execute another function, PowerOnFunc, 60 seconds after the timer has started counting.

%Timer Callback function
function [] = ftimer(~,~)
    while TimerHandle.TasksExecuted == 60
        PowerOnFunc();
        break;
    end
end

%Handle and execution
TimerHandle = timer('ExecutionMode','fixedrate','period',...
1,'tag','clockTimer','timerfcn',@ftimer);
start(TimerHandle);

However, this produces an error: 'Error while evaluating TimerFcn for timer 'timer-31'. Too many input arguments.' Any idea what could be causing the problem? Is there a simpler/more efficient way to do this?


Solution

  • Callback functions in matlab automatically get two parameters source, event you need your callback function to support this

    function [] = ftimer(src,evnt)
    

    or more realistically, since you aren't using them just do

    function [] = ftimer(~,~)
    

    as a side note you can initialize your timer on one line

    TimerHandle = timer('ExecutionMode','fixedrate','period', ...
    1,'tag','clockTimer','timerfcn',@ftimer)
    

    and another note

    TimerHandle == 60
    

    doens't work because the function doens't know what TimerHandle is. what were you trying to do with this line of code?

    EDIT

    TimerHandle == 60 is supposed to wait for 60 timeouts of the 1 second timer. It is more efficient (and probably more accurate) to set the the period to 60 seconds

    %notice I change the time period                          v here
    TimerHandle = timer('ExecutionMode','fixedrate','period',60,...
    'tag','clockTimer','timerfcn',@ftimer);
    

    Now the ftimer function will only get called every 60 seconds. If you wanted to look at a property of the timer from inside the callback function you have to use the source and event we talked about earlier

    function []=ftimer(src,evnt)
        if src.TasksExecuted == 5 
    

    edit2: typo in code above, now fixed

    Well, that works well. Just for your own knowledge, here are some of the fields src has:

    Timer Object: timer-3
    
    Timer Settings
      ExecutionMode: fixedRate
             Period: 1
           BusyMode: drop
            Running: on
    
    Callbacks
           TimerFcn: @ftimer
           ErrorFcn: ''
           StartFcn: ''
            StopFcn: ''
    

    and for more info the evnt contains:

    K>> evnt
    evnt = 
        Type: 'TimerFcn'
        Data: [1x1 struct]
    
    K>> evnt.Data
    ans = 
        time: [2015 2 19 16 37 19.3750] %this is the time the event triggered
    

    Here is an alternative way to count the number of times the code has executed, or to even keep data inside of a callback function over multiple calls. Matlab has something called persistent variables. This is like using the word 'static' in a C function (if you know C). Otherwise it just means that the variable persists even after the function ends. Here is what your code might look like if you actually wanted to know exactly how many times it executed

    %Timer Callback function
    function [] = ftimer(src,evnt)
        %this will be saved everytime the function is called, unlike normal
        %varaibles who only 'live' as long as the function is running
        persistent times_executed;
    
        %initialies persisten variable
        if (isempty(times_executed))
            times_executed = 0;
        end
    
        fprintf('executed: %d  times\n',times_executed);
    
        %increments
        times_executed = times_executed +1;
    end