Why does the signature of my method change at runtime?

1 view (last 30 days)
I have a class (made in App Designer) with a method that takes two arguments:
function updateSpeed(app, block)
end
This function is used inside a callback that is used in an event listener listening for a block's 'PostOutputs':
listener = add_exec_event_listener(block_name, 'PostOutputs', ...
@(block,event) updateSpeed(app,block));
With this code I get a 'Too many input arguments' error. However, the code runs fine if I change it to:
listener = add_exec_event_listener(block_name, 'PostOutputs', ...
@(block,event) updateSpeed(app));
If I change the method to only take one argument, and only give it one, I get the 'Too many input arguments' again. It seems that for some reason the method signature is changed to only accept one argument. I have also tried changing the method name to something obscure so I know there's not more than one method of that name.
I need to access the 'block' parameter inside 'updateSpeed', so this doesn't work. I know class methods can have more than one parameter from this page:
Why is this happening?
  4 Comments
Tommy
Tommy on 5 May 2020
Edited: Tommy on 5 May 2020
Just after taking a quick look, I'll point out that you can either reference your function with
@(block, event) app.updateSpeed(block)
or
@(block, event) updateSpeed(app, block)
but not
@(block, event) app.updateSpeed(app, block)
In your question, you used the second syntax, but in the file you uploaded, you use the third. Just to verify, do you still get an error if you change it to the first or second syntax?
(edit) copied to an answer, at least for now
Ameer Hamza
Ameer Hamza on 5 May 2020
Yes, Tommy's analysis is correct. The way you wrote it in the question caused confusion. I suggest Tommy add this as an answer so that It can be accepted and be useful for anyone else coming to this thread.

Sign in to comment.

Accepted Answer

Tommy
Tommy on 5 May 2020
Edited: Tommy on 5 May 2020
You can either reference your function with
@(block, event) app.updateSpeed(block)
or
@(block, event) updateSpeed(app, block)
but not
@(block, event) app.updateSpeed(app, block)
In your question, you used the second syntax, but in the file you uploaded, you use the third. Do you still get an error if you change it to the first or second syntax?
  6 Comments
Tommy
Tommy on 6 May 2020
Ah! Thank you very much for correcting that - I reread the note here and realized it doesn't say anything about the order!
Matthew Drummond-Stoyles
Matthew Drummond-Stoyles on 6 May 2020
You are correct about the 'StartFcn' requiring an expression and it being evaluated in the base workspace. That was the topic of my other question and it seems the only workaround is to use a global variable for the app instance. Your suggestion about requesting output would also work but I am not sure if that would work if I ran the app through the app designer window or when creating an executable.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!