Guy on Simulink

Simulink & Model-Based Design

Controlling the Location of the Generated Code and Temporary files 17

Posted by Guy Rouleau,

Simulink often needs to generate files to simulate a model. An example of this is the model reference simulation target. Today I will describe a few option to control where those files are created.

Simulation Target

If you click play in the following model:

Example model generating code

You will notice that a folder named slprj with a lot of code and 4 MEX-files will be created:

Example code generated for simulation

Simulink Preferences

If you do not want the slprj and MEX-files to be generated in your current directory, it is possible to go in the Simulink Preferences and specify a Simulation Cache Folder and a Code Generation Folder to be used instead of the local directory.

Simulink Preferences

Overriding the Simulink Preferences

Now, let's say I need to often switch between different projects, and each of those projects need to use different folders for code generation. In that case, it is possible to use Simulink.fileGenControl. For example, if I include Simulink Project in my workflow, the startup shortcut for my project could look like:

project = simulinkproject;
projectRoot = project.RootFolder;

myCacheFolder = fullfile(projectRoot, 'work');
myCodeFolder = fullfile(projectRoot, 'code');

    'CacheFolder', myCacheFolder,...
    'CodeGenFolder', myCodeFolder,...
    'createDir', true)

That way, all the code and MEX-files generated by the model will go in those folders and the current directory will remain clean.

Once you are done working on this project, you can restore the values stored in the Simulink Preferences using this line:


Don't Clean Up: Faster Initialization

Simulink checks the cached files at initialization to make sure they are up to date and match the model. This prevents re-generating files, and results in faster initialization. If you regularly work with the same models, keeping these derived files around can help you save time when switching between projects.

Now it's your turn

Let us know how you use this feature by leaving a comment here

17 CommentsOldest to Newest

Jim replied on : 1 of 17
It would be nice to be able to control the location of other generated files in a similar manner: model advisor reports, coverage reports, etc.
Guy Rouleau replied on : 2 of 17
Thanks Jim. The Model Advisor reports, like most of the files generated in the "slprj" folder respect the "Simulation cache folder" setting described in this post. I agree that it could be useful to be able to specify a location for the Coverage report, I will pass the suggestion to our development team.
ABindemann replied on : 4 of 17
I have tried this feature on multiple occasions, and sometimes it works, Often though, I get spurious errors when trying to update my Simulink model (Typically "S-Function not present in compiled state machine"). Even if I delete the slprj cache folder and try rebuilding from scratch the problem persists. If I reset the Simulink.fileGenControl command and then update the model it builds the underlying Stateflow and MATLAB Function blocks just fine. This is under R2013b.
Guy Rouleau replied on : 5 of 17
@ABindemann: I recommend contacting technical support with the full reproduction steps and the exact error message. We would like to figure out what is going wrong.
Mike replied on : 6 of 17
Neither of the above methods appears to affect the location of the files generated by the S Function Builder block. How does one control the location of those files? Thanks!
Matzix replied on : 7 of 17
May several Matlab instances running parallel share the same cache folders? If the answer is yes: What happens, if two Matlab instances both compile a similiar model at the same time? And will only be the most recent model cached or will there be compiled versions of previous models, such that they can be reused at a later time? Thank you very much!
Guy Rouleau replied on : 8 of 17
@Matzix: Multiple MATLAB instances running in parallel sharing the same caceh folder will very likely cause problems. I recommend going through this page, especially the section titled "Resolving Data Concurrency Issues":
David Yehuda replied on : 10 of 17
How to change a name of the project when using code generation. At default, it uses the simulink model name, but we want to define our own name (always)
Robin Kleinwort replied on : 11 of 17
First this worked fine, but since I had to delete and set up my windows account. I get the error: RTI does not support custom code generation folders. Do I have to change any settings in the preferences?
@Robin: Is it possible that you have third-party toolboxes installed? The term "RTI" in the error makes me believe that the error is not coming from a MathWorks toolbox. For example, is your model using dSPACE? If not, I recommend contacting technical support:
aditya joshi replied on : 13 of 17
I am using model referencing ,when building the top model "xyz.slx" using ert_shrlib model files and headers are generated in" xyz_ert_shrlib_rtw "folder but the shared files & headers are generated in "slprj folder" (code of the refernced subsystem) How can i make all the code generation file be in "xyz_ert_shrlib_rtw" folder??
Manuel Scholl replied on : 15 of 17
I get matlab error when running an optimization with 6 Simulink Models in parallel Ther error says I do not have the permission to write a simulink cache file Does anyone knows this error? Is it alternatively possible to disable the creation LG a slxc file?
@Manuel: This is probably because all the workers are trying to write to the same file at the same time. I would recommend going through the section titled "Data Concurrency Issues" in the following documentation page: Additionally, if you are using MATLAB R2017b or later, I would recommend using the parsim function instead of sim inside parfor. This should take care of this issue.
Elias Mitter replied on : 17 of 17
Hello, I hope that my question is at the proper place here. At the moment some initialization values are defined by the InitFcn pane (.mat file gets loaded) at the Model Properties/Callbacks window of my Simulink model. The embedded coder (system target file ert.tlc) is used to generate C code to run on the Raspberry Pi. This works, but to change the init values of the program I have to alter them in the model and recompile it to C. My purpose is to change the init values in a file on the target system which can be configured and loaded at execution of the program. The functionality is needed to adjust some parameters without using Simulink. Thank You!