Consider the following questions if you are unclear about what type of S-function is best for your application.
|If you...||Then use...|
|Are a MATLAB® programmer with little or no C programming experience||A Level-2 MATLAB S-function, especially if you do not need to generate code for a model containing the S-function (see Write Level-2 MATLAB S-Functions).|
|Need to generate code for a model containing the S-function||Either a Level-2 MATLAB S-function or a C MEX S-functions. Level-2 MATLAB S-functions require that you write a Target Language Compiler (TLC) file for your S-function, before generating code. See Inline MATLAB File S-Functions (Simulink Coder). C MEX S-functions, however, automatically support code generation. See Inline C MEX S-Functions (Simulink Coder).|
|Need the simulation to run faster||A C MEX S-function, even if you do not need to generate code. For complicated systems, Level-2 MATLAB S-functions simulate slower than C MEX S-functions because they call out to the MATLAB execution engine.|
|Need to implement the S-function in C, but have no previous experience writing C MEX S-functions||The S-Function Builder.|
|Are incorporating legacy code into the model||Any S-function, with the exception of a Level-1 MATLAB S-function. Consider using the Legacy Code Tool if your legacy function calculates only outputs, not dynamic states (see Integrate C Functions Using Legacy Code Tool). Otherwise, consider using the S-Function Builder. If you need to call the legacy code during simulation, do not use a Level-2 MATLAB S-function because they call legacy code only through their TLC files.|
|Need to generate embeddable code for an S-function that incorporates legacy code||The Legacy Code Tool if your legacy function calculates only outputs. Otherwise, use a handwritten C MEX S-function or the S-Function Builder.|