Guy on Simulink

Simulink & Model-Based Design

Controlling the Location of the Generated Code and Temporary files 10

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');

Simulink.fileGenControl('set',...
    '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.

Now it's your turn

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

10 CommentsOldest to Newest

Jim replied on : 1 of 10

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 10

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 10

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 10

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

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 10

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!

David Yehuda replied on : 10 of 10

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)

Add A Comment

Your email address will not be published. Required fields are marked *

*

Preview: hide