S-Function Types

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 (seeWrite 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. SeeInline MATLAB File S-Functions(Simulink Coder). C MEX S-functions, however, automatically support code generation. SeeInline 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 TheS-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 (seeIntegrate 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.
为一个功能需要生成可嵌入代码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.