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.
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.
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:
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
12 CommentsOldest to Newest
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.
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.
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.
@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.
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!
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!
@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”:
Hi Guy! How can it reduce the size of a generated .mex? Thanks!
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)
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: https://www.mathworks.com/support/contact_us