Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

What Type of S-Function Should You Use?

Consider the following questions if you are unclear about what type of S-function is best for your application.

If you...Then use...
Are an M programmer with little or no C programming experienceA Level-2 M-file S-function, especially if you do not need to generate code for a model containing the S-function (see Writing S-Functions in M).
Need to generate code for a model containing the S-functionEither a Level-2 M-file S-function or a C MEX S-functions. Level-2 M-file S-functions require that you write a Target Language Compiler (TLC) file for your S-function, before generating code. C MEX S-functions, however, automatically support code generation.
Need the simulation to run fasterA C MEX S-function, even if you do not need to generate code (see Writing S-Functions in C). For complicated systems, Level-2 M-file S-functions simulate slower than C MEX S-functions because they call out to the MATLAB interpreter.
Need to implement the S-function in C, but have no previous experience writing C MEX S-functionsThe S-Function Builder.
Are incorporating legacy code into the modelAny S-function, with the exception of a Level-1 M-file S-function. Consider using the Legacy Code Tool if your legacy function calculates only outputs, not dynamic states (see Integrating Existing C Functions into Simulink Models with the 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 M-file S-function because they call legacy code only through their TLC files.
Need to generate embeddable code for an S-function that incorporates legacy codeThe Legacy Code Tool if your legacy function calculates only outputs. Otherwise, use a handwritten C MEX S-function or the S-Function Builder.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS