# Controlling the Location of the Generated Code and Temporary files12

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:

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

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.

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:

Simulink.fileGenControl('reset')


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.

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

Jim replied on : 1 of 12

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 12

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.

jiong replied on : 3 of 12

givemefreegems

ABindemann replied on : 4 of 12

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 12

@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 12

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 12

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!

JLND replied on : 9 of 12

Hi Guy! How can it reduce the size of a generated .mex? Thanks!

David Yehuda replied on : 10 of 12

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 12

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?

Guy Rouleau replied on : 12 of 12

@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: https://www.mathworks.com/support/contact_us