How can I initialize internal information from an input signal in an S-function in Simulink?

5 views (last 30 days)
I use the mdlInitializeConditions function in my S-function to initialize a work vector element from the current input signal. In some cases I experience unexpected behaviour because the S-function's work vector element is not initialized with the expected value.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Feb 2011
It is not recommended to access the S-function's input signal for initializing internal information during the mdlInitializeConditions function in Simulink. For non-virtual blocks other than Constant or Initial Condition (IC) the S-function might not be able to read the input signal during initialization phase resulting in unexpected behaviour.
You can work around this by setting an internal flag in mdlInitializeConditions. In mdlOutputs you could check if this flag is set and initialize the work vector with the current input signal. Then unset the flag.
Please note that you have to set ssSetInputPortDirectFeedThrough for the appropriate input when using this workaround, because you are accessing the input signal during the mdlOutputs function.
Attached you can find a simple example showing an S-function that creates a ramp signal based on the initial value from the input signal during initialization of the S-function.

More Answers (0)

Products


Release

R2007a

Community Treasure Hunt

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

Start Hunting!