Use of variables in Configuration Parameters -> Custom Code

11 views (last 30 days)
I have a simulink model that uses s-functions and also I compile it using Simulink Coder. The problem is that I have additional build information, especially, Custom Code Include Directories ('CustomInclude') and Custom Code Libraries ('CustomLibrary') that are machine specific and I would like to define these paths indirectly using an environment variable, workspace variable, user function or any other method.
I'm trying to avoid using a script to change there parameters (so the model does not "get dirty" when there is no real change).
The s-function builder block has a similar feature where one can use enviroment variables. I was able to use it successfully in another case.
Can I use any type of variable in the Custom Code Parameters?
  1 Comment
Braulio Horta
Braulio Horta on 11 Oct 2024
Recently, I realized I can use environment variables enclosed in % (i.e. windows syntax) in these parameters. It helps but does not solve my problem entirely.

Sign in to comment.

Accepted Answer

Pablo Romero
Pablo Romero on 7 Feb 2025
Edited: Pablo Romero on 7 Feb 2025
You can enclose expressions with dollar signs ($...$) in Custom Code or Simulation Target within the model's configuration parameters to:
  • Evaluate the expressions in the MATLAB workspace. For example, suppose that CustomCodeFolder is a variable that you define in the MATLAB workspace as "module1". If you specify your custom code files using the path name .\work\source\$CustomCodeFolder$, then the model searches for the custom code files in the folder .\work\source\module1.
  • Use MATLAB functions that generate paths to your custom code files. The functions must be on the MATLAB search path. For example, suppose that a function myFolderFcn returns the path to your custom code folder by running the command fileparts(mfilename('fullpath')). If you specify a custom code file using the path name $myFolderFcn$/myCustomFile, then the model searches for myCustomFile in the folder returned by myFolderFcn.

More Answers (1)

Abhinav Aravindan
Abhinav Aravindan on 9 Oct 2024
I understand that you are looking for a way to list directories and libraries in the "Custom Code" section of “Configuration Parameters” using variables as they are system specific. One possible way to do this is by using MATLAB workspace variables and the "PostLoadFcn" model callback. This can be done by the following steps:
  1. Define the required directory paths and libraries to the MATLAB workspace.
dirCI = "space-separated list of folders"
libsCI = "space-separated list of library files"
2. In Simulink, you can go to Modelling -> Setup -> Model Properties and add the following to “PostLoadFcn” model callback as follows:
set_param('modelName', 'CustomInclude', dirCI);
set_param('modelName', 'CustomLibrary', libCI);
The model will add the directories and libraries set in the MATLAB workspace after the model is opened. However, it is to be noted that if the paths and library variables are changed in the MATLAB workspace, you will need to close the model and reopen it to update the custom code inclusions.
Please find the related documentation below for your reference.
I hope this helps!
  1 Comment
Braulio Horta
Braulio Horta on 11 Oct 2024
Edited: Braulio Horta on 11 Oct 2024
It works but, as I mentioned in the original question, I was thinking about an approach where I don't have to change the parameter.

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!