| Target Language Compiler | ![]() |
Wrapper Inlined S-Function Example
The following diagram illustrates inlining an S-function as a wrapper. The algorithm is directly called from the generated model code, removing the S-function overhead but maintaining the user function.
This is the inlining TLC file for a wrapper version of the timestwo block.
%implements "timestwo" "C" %% Function: BlockTypeSetup ================================== %% %function BlockTypeSetup(block, system) void %% Add function prototype to models header file %<LibCacheFunctionPrototype... ("extern void mytimestwo(real_T* in, real_T* out,int_T els);")> %% Add file that contains "myfile" to list of files to be compiled %<LibAddToModelSources("myfile")> %endfunction %% Function: Outputs ========================================== %% %function Outputs(block, system) Output /* %<Type> Block: %<Name> */ %assign outPtr = LibBlockOutputSignalAddr(0, "", "", 0) %assign inPtr = LibBlockInputSignalAddr(0, "", "",0) %assign numEls = LibBlockOutputSignalWidth(0) /* Multiply input by two */ mytimestwo(%<inPtr>,%<outPtr>,%<numEls>); %endfunction
Analysis
The function BlockTypeSetup is called once for each type of block in a model; it doesn't produce output directly like the Outputs function. Use BlockTypeSetup to include a function prototype in the model.h file and to tell the build process to compile an additional file, myfile.c.
Instead of performing the multiply directly, the Outputs function now calls the function mytimestwo. So, all instances of this block in the model will call the same function to perform the multiply. The resulting model function, MdlOutputs, then becomes
void MdlOutputs(int_T tid) { /* S-Function Block: <Root>/S-Function */ /* Multiply input by two */ mytimestwo(&rtB.Constant_Value,&rtB.S_Function,1); /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.S_Function; }
Summary
This section has been a brief introduction to the model.rtw file and the concepts of inlining an S-function using the Target Language Compiler. Contents of model.rtw, and model.rtw, contain more details of the model.rtw file and its contents. Inlining S-Functions, and A TLC Tutorial, contain details on writing TLC files, including a comprehensive tutorial.
| Fully Inlined S-Function Example | A TLC Tutorial | ![]() |