MATLAB: How to prepare a model containing an S-function to be referenced by another model

gmakesimulink

I have a referenced model that contains an S-Function. However, when I try to simulate the top level or parent model, I receive the following error message:
 
C:\Temp\RefBmsf.obj RefBlib.lib ..\..\..\slprj\sim\sharedutils\rtwshared.lib D:\Applications\MATLAB\R2006a\rtw\c\lib\win32\rtwlibrtwsfcnvc.lib D:\Applications\MATLAB\R2006a\extern\lib\win32\microsoft\libfixedpoint.lib Creating library C:\Templib1079.x and object C:\Templib1079.exp RefBlib.lib(RefB.obj) : error LNK2019: unresolved external symbol MySFcn referenced in function _mrRefB ..\..\..\RefBmsf.mexw32 : fatal error LNK1120: 1 unresolved externals D:\APPLICATIONS\MATLAB\R2006A\BIN\MEX.PL: Error: Link of '..\..\..\RefBmsf.mexw32' failed. '-e' is not recognized as an internal or external command, operable program or batch file. NMAKE : fatal error U1077: 'D:\Applications\MATLAB\R2006a\bin\win32\mex' : return code '0x1' Stop. The make command returned an error of 2 'An error occurred during the call to make

Best Answer

This change has been incorporated into the documentation in Release 2010b (R2010b). For previous releases, read below for any additional information:
The ability to simulate a model that references a submodel that contains an S-function has been implemented in Simulink 7.0 (R2007b). You can simulate such a model in Normal mode, which does not involve any code generation. For previous releases, read the following:
As of MATLAB 7.4 (R2007a), you can build your model into a shared library. The model can then be called by other models. For more information on this, see the following documentation page:
web([docroot,'/toolbox/ecoder/rn/bq2e5jb.html'])
For Simulink 6.4 (R2006a) and previous versions of Simulink, a referenced model can contain only inlined S-functions. In Simulink 6.5 (R2006b), you can simulate a model that references models that contain non-inlined S-functions. However, generating code for standalone targets using the Real-Time Workshop targets requires inlined S-functions.
A referenced model requires inlined S-functions in the following cases:
1. The model uses a variable-step solver.
2. The model is referenced more than once in the model reference hierarchy. The workaround in this case is to make copies of the referenced model, assign different names to the copies, and reference the copies in the model reference hierarchy.
3. The S-function was generated by Real-Time Workshop.
You can find information on model referencing limitations at the following documentation page:
web([docroot,'/toolbox/simulink/ug/bqonbts-1.html'])
To inline an S-Function for compatibility with Model Referencing in Simulink, perform the following steps:
1. Create a TLC file for the S-Function that performs the necessary operations. For inlining S-Functions, refer to the following documentation page:
web([docroot,'/toolbox/rtw/tlc/f7667dfi7.html'])
2. Ensure that the S-Function contains the option to select the TLC file during code generation by using the flag "SS_OPTION_USE_TLC_WITH_ACCELERATOR". Simulink inlines the S-function only if the S-function sets the "SS_OPTION_USE_TLC_WITH_ACCELERATOR" flag.
3. If you are using a wrapper-inlined S-Function, i.e., if the TLC refers to a C-File, ensure that this C-File is included in the TLC. There are two ways to ensure that the TLC can find the C-File:
a. Specify the file name in the TLC as follows: %<LibAddToModelSources("MySFcn")>
b. Specify the C-file name in the S-Function modules in the referenced model. This is the preferred method. For more information on this topic refer to the following documentation pages:
web([docroot,'/toolbox/rtw/tlc/bp6sft1.html'])
web([docroot,'/toolbox/rtw/ug/bp67s_y-3.html'])
4. Next, ensure that the TLC can find the function definition for the referenced function. This can be done in two ways:
a. Include a header file that contains the function definition in the TLC as follows"
%openfile buffer

#include "Myheader.h"
%closefile buffer

where Myheader.h contains the function definition, for example:
float MySFcn( void );
b. Include the function definition in the TLC file. You may need to use this if you are using the LCC compiler. For example:
%openfile buffer
float MySFcn( void );
%closefile buffer
5. Mex the S-Function using the MEX command:
mex sfcn_name.c
Attached is an example that demonstrates referencing a model that contains an inlined S-Function.