MATLAB Answers

Simulink Library Code Generation

52 views (last 30 days)
Davide Frey
Davide Frey on 12 Apr 2019
Commented: Davide Frey on 24 Apr 2019
Good morning,
I have various models in Simulink which refers to the same custom made library. When I generate the code for one of these models, the library blocks are generated in a separated file and the functions take the following name:
As I need to generated the code separately for any of my models, I was wondering if it is possible to build a single file for all the library blocks, which uses as function names only the library block name, without including the name of the model itself. The goal would be to have two single .cpp and .h files for all the different models.


Sign in to comment.

Accepted Answer

Mark McBroom
Mark McBroom on 15 Apr 2019
Yes this is possible, but only if you ahve an Embedded Coder license. You must first configure the library blocks as follows:
Enable code reuse for the library block:
  1. In the library, right-click the MATLAB Function block my_conv_filter and select Block Parameters (Subsystem) from the context menu.
  2. In the Function Block Parameters dialog box, set these parameters:
  • Select the Treat as atomic unit check box.
  • In the Function packaging field, select Reusable function from the drop-down menu.
The, you must configure your model to place the generated code in a "shared" location that all models can access:
  • Select "Simulink" from Simulink menu
  • Select "Model Configuration Parameters"
  • Select the "Code Generation" tab on left hand side
  • Set System Target file to "ert.tlc"
  • Select "Interface" on left hand side
  • Set Shared Code Placement to "Shared Location"
Now, when you generate code, the .c and .h file will be in the directory /slprj/ert/_sharesutils

  1 Comment

Davide Frey
Davide Frey on 24 Apr 2019
Not exactly the complete solution to my problem. This allows me to generate the library functions in a separated code, though the naming of the functions is the problem. Indeed the functions take the name of the builded model plus the name of the subsystem (as I mentioned before), something like this:
But in order to use this generated code also with other models (modelNr2, modelNr3, ...), I need to generate a function in a shared location with the following name:
without any referencing to the original model.
Nevertheless given your inputs, I managed to solve my problem also by setting both "Function name options" and "File name options" to "User specified" (under Block Parameters > Code Generation). This allowed me to hard type the name of the functions.
Thank you very much for your help!

Sign in to comment.

More Answers (0)

Sign in to answer this question.