How can I use set_param to set StartFcn from within a class method?

I am trying to use
set_param(app.Model,'StartFcn','app.registerListeners')
but the code when run inside StartFcn doesn't recognize 'app' or 'registerListeners'. 'app' is a parameter of the method this is called from, and 'registerListeners' is a public method in the same class.
I have tried:
  • Using a callback instead of putting the third argument in single quotes - says 'Invalid setting for block diagram parameter 'StartFcn'

Answers (1)

Can you add the error being thrown when you use 'app.registerListeners(app)' as the third parameter?
If this is the error,
The class app has no Constant property or Static method named 'registerListeners'.
you may try to convert registerListeners function to static to use it as a start function. Also, try to check if app and model are in same folder or add the folder containing app to the MATLAB path.

6 Comments

Sorry, I made a mistake in the syntax, the question is updated and should have the correct syntax. The problem is about how to access the 'app' instance (preferably without using a global variable).
I can make the function static but it would have the same problem; I can't figure out how to access the 'app' instance since the 'StartFcn' code is called from the workspace not from inside the app's code.
I have tried with a static method and it worked. Try to check if app is on the MATLAB path. If this is not the issue, Can you attach the app and model? I will try to figure out the issue.
Thank you for your help. The method has to be non-static in order to access certain Properties and other methods of the app instance. I can attach the code and model but the problem is more about if there is a way to access the app instance from Simulink's StartFcn callback and not that the code is giving an error that I don't know the cause of.
While possibly not the best solution, is the following an option?
assignin('base', 'app', app);
set_param(app.Model, 'StartFcn', 'app.registerListeners');
Yes but that is basically the same as what I am currently doing (using a global variable). It works but everything I've read says global variables are never the answer if you want to write clean code. It's surprising that this hasn't been asked before, since I am only at the stage of trying to link the user interface to my Simulink model.

Sign in to comment.

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!