0

The problem with a timer in the MatLab App Designer. It return an error: "Error while evaluating TimerFcn for timer.. Too many input arguments"

my code:

app.t.TimerFcn = @app.timerFunction;
function timerFunction(app, ~, ~)

    %something

end

I've been looking for a solution. I also tried this:

app.t.TimerFcn =  @(app, ~, ~)app.timerFunction
timerFunction(app, ~, ~)
app.t.TimerFcn =  @(~,~)app.timerFunction
timerFunction(app)

Any help would be appreciated.

William Davis
  • 43
  • 1
  • 6
  • @Adriaan It's useful in cases like this where MATLAB is going to give you multiple inputs since it's an event, but you don't care about them. – Wolfie Oct 27 '20 at 14:10
  • @Adriaan: if you write a callback function, it needs to have the right signature, the right number of input arguments. If you don’t need all those arguments in your function, you can use `~` to avoid naming them. This is recommended syntax. – Cris Luengo Oct 27 '20 at 14:11
  • 1
    Please show the full stack trace from the error message. Maybe this is about a different TimerFunction, or maybe you’re calling a different function than you think. The timer callback expects two input arguments. – Cris Luengo Oct 27 '20 at 14:19
  • @Cris Luengo Too many input arguments. Error while evaluating TimerFcn for timer 'timer-61' – William Davis Oct 27 '20 at 20:47
  • `timer-61` indicates you have constructed 61 timers. Are they all still up? This might be a different timer altogether that's erroring out. Consider clearing them, to avoid confusion: type `clear all` at the MATLAB command prompt (or restart MATLAB). – Cris Luengo Oct 27 '20 at 21:56

3 Answers3

2

You can just use varargin and worry less about the number of input arguments which the event is throwing, i.e.

function timerFunction( app, varargin )
    % stuff
end

This will also help you debug and see how many inputs MATLAB is in fact trying to pass, by looking at the contents of varargin.

In a similar way to your other attempts you can use varargin in your function handle too, although this doesn't allow for the same debugging

app.t.TimerFcn = @(varargin) app.timerFunction;
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • Just tried it. Didn't help( But if I put `dbstop()` in the `function timerFunction( app, varargin )`, it says "Not enough input arguments" – William Davis Oct 27 '20 at 19:33
  • @William Where does it say that? If you can stop *inside* the callback then are you getting the error for your call to `app.readAppData`? Looking at your question again it looks like you're passing `app` in twice, you should do **either** `app.readAppData(app.serRead)` **or** `readAppData(app, app.serRead)`. If this is actually your issue please let me know and I'll update the answer. – Wolfie Oct 27 '20 at 20:53
  • Suddenly, I have found a solution...But I really don't understand what happens here. `app.t.TimerFcn = @(x,y)timerFunction(app);` `function timerFunction(app)` and you were right, there was one extra error in the function's body. – William Davis Oct 27 '20 at 21:30
0

The found solution is:

callback function:

function timerFunction(app)
    % stuff
end

timer settings

app.t.TimerFcn =  @(x,y)timerFunction(app);
William Davis
  • 43
  • 1
  • 6
  • I think you must have had wires crossed whilst originally debugging this issue. Doing this should be equivalent to your original `@app.timerFunction` with `function timerFunction(app,~,~)`, or equivalent to replacing `x` and `y` with `~`. In future, including a [mcve] in your question helps clarify specific issues. – Wolfie Oct 28 '20 at 08:07
  • Thank you for your comment. I have reduced the code for a better understanding:) Good luck. – William Davis Oct 28 '20 at 21:13
0

Had the same problem and just wanted to let you know that this error can be a bit misleading. In my case, I called a function via a timer, and I had a normal function call in that callback function:

T = timer('TimerFcn', @(~,~) this.callback, 'StartDelay', waitTime);

...

function callback(this)
  ...
  this.doStuff(x,y);
end

function doStuff(x, y)
  ...
end

which of course should have been:

function doStuff(this, x, y)
  ...
end

So the problem didn't have anything to do with the timer, but was just an error in some other part that was executed by the timer callback function.

DocRobson
  • 125
  • 1
  • 1
  • 9