| 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; }
Because ssSetErrorStatus does not generate exceptions, using it to report errors in your S-function is preferable to using mexErrMsgTxt. The mexErrMsgTxt function uses exception handling to terminate S-function execution and return control to Simulink. To support exception handling in S-functions, Simulink must set up exception handlers prior to each S-function invocation. This introduces overhead into simulation.
| Processing Frame-Based Signals | Exception Free Code | ![]() |
© 1994-2005 The MathWorks, Inc.