User`s guide
3 Writing S-Functions As C-MEX files
3-12
• simulink.c is included if the file is being compiled int o a MEX-file.
•
cg_sfun.h is included if the file is b eing used in conjunction with the
Real-Time Workshop to produce a stand-alone or real-time executable.
Note This trailer code must not be in the body of any S-function routine.
Conditionally Compiling S-Functions
S-functions can be compiled in one of three modes id entified by the presence of
one of the following defines.
•
MATLAB_MEX_FILE — Indicates that the S-function is being built as a
MEX-file for use with Simulink.
•
RT — Indicates th at the S-function is being built w ith the Real -Time
Workshop generated cod e for a real-time applicati on using a fixed-step
solver.
•
NRT — Indicate s that the S-fu nct io n is being built with t h e Real-Time
Workshop generated cod e for a nonreal -t ime applicat ion using a
variable-step solv er.
Error Handling
When working with S-functions, it is important to handle unexpected events
correctly such as invalid parameter values.
In the
timestwo example above, there was no need for significant error
handling. The only piece of code that performed error handling was the early
return in mdlInitializeSizes. Here Simulink issues a parameter mismatch
if you entered parameters in the dialog box. If your S-function has parameters
whose contents you need to validate, use the following technique to report
errors encountered:
ssSetErrorStatus(S,"error encountered due to ...");
return;