| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
| On this page… |
|---|
Noninlined S-functions are identified by the absence of an sfunction.tlc file for your S-function. The filename varies depending on your platform. For example, on a 32–bit Microsoft Windows system, the file name would be sfunction.mexw32. Type mexext in the MATLAB Command Window to see which extension your system uses.
The MEX-file cannot call MATLAB functions.
If the MEX-file uses functions in the MATLAB External Interface libraries, include the header file cg_sfun.h instead of mex.h or simulink.c. To handle this case, include the following lines at the end of your S-function:
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */ #include "simulink.c" /* MEX-file interface mechanism */ #else #include "cg_sfun.h" /* Code generation registration function */ #endif
Use only MATLAB API function that the code generator supports, which include:
| mxGetEps |
| mxGetInf |
| mxGetM |
| mxGetN |
| mxGetNaN |
| mxGetPr |
| mxGetScalar |
| mxGetString |
| mxIsEmpty |
| mxIsFinite |
| mxIsInf |
MEX library calls are not supported in generated code. To use such calls in MEX-file and not in the generated code, conditionalize the code as follows:
#ifdef MATLAB_MEX_FILE #endif
Use only full matrices that contain only real data.
Do not specify a return value for calls to mxGetString . If you do specify a return value, the MEX-file will not compile correctly. Instead, use the function's second input argument, which returns a pointer to a string.
Make sure that the #define s-function_name statement is correct. The S-function name that you specify must match the S-function's filename.
Use the data types real_T and int_T instead of double and int, if possible. The data types real_T and int_T are more generic and can be used in multiple environments.
Provide the Real-Time Workshop build process with the names of all modules used to build the S-function. You can do this by using the Real-Time Workshop template make file or the set_param function. For example, suppose you build your S-function with the following command:
mex sfun_main.c sfun_module1.c sfun_module2.c
You can then use the following call to set_param to include all the required modules:
set_param(sfun_block, "SFunctionModules","sfun_module1 sfun_module2')
Parameters to noninlined S-functions can be of the following types only:
Double precision
Characters in scalars, vectors, or 2-D matrices
For more flexibility in the type of parameters you can supply to S-functions or the operations in the S-function, inline your S-function and consider using an mdlRTW S-function routine.
Use of other functions from the MATLAB matrix.h API or other MATLAB APIs, such as mex.h and mat.h, is not supported. If you call unsupported APIs from an S-function source file, compiler errors occur. See the file matlabroot/rtw/c/src/rt_matrx.h(.c) for details on supported MATLAB API functions.
If you use mxGetPr on an empty matrix, the function does not return NULL; rather, it returns a random value. Therefore, you should protect calls to mxGetPr with mxIsEmpty.
![]() | Introduction to S-Functions for Code Generation | Writing Wrapper S-Functions | ![]() |

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 |