Including Out-of-Scope Parameters within M S-functions
Show older comments
Hello community.
I've recently picked up a project from a PhD student who has been working on an estimator within Simulink. He uses Matlab S-functions for the estimator and model block, and has a separate script where he defines 100+ constant parameters.
As it stands, in order for the script to be able to recognize these parameters he reruns this parameter script during each callback function (initialize, update, outputs, etc.). This takes up 40% of my simulation time, and I'd like to remove it.
Is there a way to include common parameters within the S-Function without passing them, preferably from the workspace? They are frequently tuned, so its nice having a script file that sets them all, as opposed to an incredibly long set of arguments. Or I could save them all as a .mat file, but then I'd like to be able to just load it once somewhere and go rather than reload it during every callback function. Thanks!
Accepted Answer
More Answers (1)
Walter Roberson
on 22 Oct 2012
1 vote
You could read them at initialization time, and store them in persistent variables.
I am seeing hints that there is a new-ish approach to this involving "work vectors". I have seen a few examples posted, but I have not read about them, so I do not know what the advantages over persistent variables would be.
2 Comments
Kaustubha Govind
on 23 Oct 2012
Persistent variables should not be used to store state in S-functions, because this can cause issues if there are multiple instances of the same S-function in a model - this is where work vectors come in - they are essentially similar to persistent variables, but are scoped to the instance of the S-function. However, for this particular situation, I think registering tunable parameters on the S-function is the right approach.
Jeremy
on 25 Oct 2012
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!