How can I use batch files to create an executable for a model which has model references?

8 views (last 30 days)
Let's say we have models r1 and r2 where r1 is a model reference inside r2. When we generate code for r2 via Simulink coder - Generate code only, everything works fine and we get the artifacts. In the artifacts, we have a 'r2.bat' file in 'r2_grt_rtw\'.
When we run 'r2_grt_rtw\r2.bat' the make process fails as it does not know how to make or find 'r1' model libraries. Now, the 'r2_grt_rtw\r2.bat' build will pass if we manually make 'r1.bat' found in 'slprj\grt\r1' and we also need to build the shared library using 'slprj\grt\_sharedutils\rtwshared.bat'.
  • How can I automate this such that the making of model references and shared libraries links these two, i.e. executing 'slprj\grt\r1\r1.bat' and 'slprj\grt\_sharedutils\rtwshared.bat' becomes part of 'r2_grt_rtw\r2.bat'?
  • If the first question is not feasible then how can we add custom hooks into 'r2_grt_rtw\r2.bat' or 'r2_grt_rtw\' through Simulink Coder, to achieve the execution of 'slprj\grt\r1\r1.bat', 'slprj\grt\_sharedutils\rtwshared.bat' and potentially more model references batch scripts? My assumption here is that hooks in a makefile can help us in executing make commands for referenced models.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Jun 2023
Edited: MathWorks Support Team on 22 Jun 2023
Starting in R2020b, a new API has been introduced called 'codebuild' which can successfully generate a CMakeLists.txt for the complete application.
Alternatively, you can use the 'PackNGo' feature to obtain the list of files. You would need to use 'packType = hierarchical' and then add the makefiles to the 'buildInfo' before calling 'PackNGo'. Please see the following documentation page for more information: 
If these options do not work for your use case, please consider the following points for achieving this manually:
Through a suitable hook like "postCodeGenCommand" you can programmatically construct an overarching script that calls the individual makefiles and bat scripts. 
You can access the list of referenced models by querying 'buildInfo.ModelRefs'. 'BuildInfo' is one of the auto-generated artifacts in the form of a MAT file, which you can load and access its data.
Lastly, please see the following pseudocode that will compose paths of Model Reference libraries (and 'rtwsharedlib'), which should serve as a template to achieve the script you are aiming for.
% Extract referenced model info
mdls = args.buildInfo.ModelRefs;
% Find the path of the directory code generation was started from
startDir = args.buildInfo.Settings.LocalAnchorDir;
% Initialize empty cell for path info
paths = cell(size(mdls));
% Additional Code for the Relative Directory
% Determine your current working directory
currentDir = pwd;
% Remove the starting directory from the current directory, this will leave
% only the path from the starting directory to the current folder.
pathFromAnchor = currentDir(2+length(startDir):end);
% Extract all the folders between the starting directory and the current
% directory
folders = strsplit(pathFromAnchor,'\');
% Create the relative path to the start directory based on the number of
% folders between the start and current directories.
relativePath = repmat('../',1,length(folders));
% If there are model references loop over them and construct the path for
% each reference
if ~isempty(mdls)
for i = 1:length(mdls)
libname = mdls(i).Name;
fullpath = replace(mdls(i).Path,'$(START_DIR)\', relativePath);
paths{i} = [fullpath,'\',libname];

More Answers (0)


Find more on Configure and View Diagnostics in Help Center and File Exchange


No tags entered yet.




Community Treasure Hunt

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

Start Hunting!