You can implement your S-function in one of five ways:
A Level-1 MATLAB S-function provides a simple MATLAB interface to interact with a small portion of the S-function API. Level-2 MATLAB® S-functions supersede Level-1 MATLAB S-functions.
A Level-2 MATLAB S-function provides access to a more extensive set of the S-function API and supports code generation. In most cases, use a Level-2 MATLAB S-function when you want to implement your S-function in MATLAB.
A handwritten C MEX S-function provides the most programming flexibility. You can implement your algorithm as a C MEX S-function or write a wrapper S-function to call existing C, C++, or Fortran code. Writing a new S-function requires knowledge of the S-function API and, if you want to generate inlined code for the S-function, the Target Language Compiler (TLC).
The S-Function Builder is a graphical user interface for programming a subset of S-function functionality. If you are new to writing C MEX S-functions, you can use the S-Function Builder to generate new S-functions or incorporate existing C or C++ code without interacting with the S-function API. The S-Function Builder can also generate TLC files for inlining your S-function during code generation with the Simulink® Coder™ product.
The Legacy Code Tool is a set of MATLAB commands that helps you create an S-function to incorporate legacy C or C++ code. Like the S-Function Builder, the Legacy Code Tool can generate a TLC file to inline your S-function during code generation. The Legacy Code Tool provides access to fewer of the methods in the S-function API than the S-Function Builder or a handwritten C MEX S-function.
The following sections describe the uses, features, and differences of these S-function implementations. The last section compares using a handwritten C MEX S-function, the S-Function Builder, and the Legacy Code Tool to incorporate an existing C function into your Simulink model.