# Controlling the Location of the Generated Code and Temporary files

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.