Customize Init function of generated code from C MEX s-function.

7 views (last 30 days)
Hello All,
I need help regarding code generation from level-2 c mex s-function. I have written an s-function c-file and a TLC file for code generation.
Here is my TLC file:
%implements "my_fun" "C"
%function BlockTypeSetup(block, system) void
%openfile buffer
some function prototype code;
%closefile buffer
%<LibCacheFunctionPrototype(buffer)>
%openfile buffer
#include "Some_Header.h"
%closefile buffer
%<LibCacheIncludes(buffer)>
%endfunction %%BlockTypeSetup
%function Outputs(block, system) Output
/* %<Type> Block: %<Name> */
%assign y = LibBlockOutputSignal(0, "", "", 0)
%assign SignalName = SFcnParamSettings.SignalName
%assign IsArray = CAST("Number",SFcnParamSettings.IsArray)
%assign ArrayIdx = CAST("Number",SFcnParamSettings.ArrayIdx)
%if (IsArray == 1)
%<y> = ApplicationShadow->%<SignalName>[%<ArrayIdx>];
%else
%<y> = ApplicationShadow->%<SignalName>;
%endif
%endfunction %%Outputs
Code generation works fine. It generates two functions: Init and Step.
I want to add some custom code lines in Init function. How do I do that? Where do I add those lines in TLC code? Which %<LibCache*******(buffer)> method do I use?
NOTE:I have multiple instances of this s-function block in my model and I want to put this custom code just once in Init function. At the moment I put these custom code lines via Model Congicuration Parameters->Code Generation->Custom Code->Insert custom C code in generated:->Initialize function
I hope my question is clear. Amy help is greatly appreciated.
Thanks, Nirav

Accepted Answer

Mark McBroom
Mark McBroom on 7 Apr 2018
You select the appropriate buffer based on where in the C file you want the code placed. For example, if your are adding a '#include "myfun.h" to the C code, you would place it in the LibCacheIncludes buffer. This link describes the different buffers available. The callback BlockTypeSetup() is only called once, regardless of how many instances of the s-function appear in your model, so your custom code should go here. You can also use the custom code option you described, but placing code in the TLC file is the recommended approach... otherwise users of your block have to remember to place code in custom code.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!