Why does the signature of my method change at runtime?

3 次查看(过去 30 天)
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 个评论
Tommy
Tommy 2020-5-5
编辑:Tommy 2020-5-5
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 2020-5-5
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.

请先登录,再进行评论。

采纳的回答

Tommy
Tommy 2020-5-5
编辑:Tommy 2020-5-5
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 个评论
Tommy
Tommy 2020-5-6
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
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by