Writing S-Functions | ![]() ![]() |
Handling Errors
When working with S-functions, it is important to handle unexpected events such as invalid parameter values correctly.
If your S-function has parameters whose contents you need to validate, use the following technique to report errors encountered.
Note that the second argument to ssSetErrorStatus
must be persistent memory. It cannot be a local variable in your procedure. For example, the following causes unpredictable errors.
mdlOutputs() { char msg[256]; /* ILLEGAL: should be "static char msg[256];" */ sprintf(msg,"Error due to %s", string); ssSetErrorStatus(S,msg); return; }
The ssSetErrorStatus
error-handling approach is the suggested alternative to using the mexErrMsgTxt
function. The function mexErrMsgTxt
uses exception handling to immediately terminate S-function execution and return control to Simulink. In order to support exception handling inside S-functions, Simulink must set up exception handlers prior to each S-function invocation. This introduces overhead into simulation.
![]() | Function-Call Subsystems | Exception Free Code | ![]() |