{"id":3863,"date":"2015-01-16T14:37:46","date_gmt":"2015-01-16T19:37:46","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=3863"},"modified":"2019-02-01T09:48:30","modified_gmt":"2019-02-01T14:48:30","slug":"controlling-the-location-of-the-generated-code-and-temporary-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2015\/01\/16\/controlling-the-location-of-the-generated-code-and-temporary-files\/","title":{"rendered":"Controlling the Location of the Generated Code and Temporary files"},"content":{"rendered":"<p>Simulink often needs to generate files to simulate a model.  An example of this is the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/model-reference-simulation-targets.html (link no longer works)\">model reference simulation target<\/a>. Today I will describe a few option to control where those files are created.<\/p>\r\n\r\n<p><strong>Simulation Target<\/strong><\/p>\r\n\r\n<p>If you click play in the following model:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q2\/exampleModelRef.png\" alt=\"Example model generating code\" \/><\/p>\r\n\r\n<p>You will notice that a folder named <tt>slprj<\/tt> with a lot of code and 4 <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mex.html\">MEX-files<\/a> will be created:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q2\/Simcode.png\" alt=\"Example code generated for simulation\" \/><\/p>\r\n\r\n\r\n<p><strong>Simulink Preferences<\/strong><\/p>\r\n\r\n<p>If you do not want the <tt>slprj<\/tt> and MEX-files to be generated in your current directory, it is possible to go in the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/simulink-preferences-window-main-pane.html (link no longer works)\">Simulink Preferences<\/a> and specify a <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/simulink-preferences-window-main-pane.html#bslolo1-1 (link no longer works)\">Simulation Cache Folder<\/a> and a <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/simulink-preferences-window-main-pane.html#bslol74-1 (link no longer works)\">Code Generation Folder<\/a> to be used instead of the local directory.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q2\/simulinkPrefs.png\" alt=\"Simulink Preferences\" \/><\/p>\r\n\r\n<p><strong>Overriding the Simulink Preferences<\/strong><\/p>\r\n\r\n<p>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 <a href=\"https:\/\/www.mathworks.com\/help\/rtw\/ref\/simulink.filegencontrol.html\"><tt>Simulink.fileGenControl<\/tt><\/a>. For example, if I include <a title=\"https:\/\/www.mathworks.com\/discovery\/simulink-projects.html (link no longer works)\">Simulink Project<\/a> in my workflow, the <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2017a\/simulink\/ug\/automate-startup-tasks-with-shortcuts.html (link no longer works)\">startup shortcut<\/a> for my project could look like:<\/p>\r\n\r\n<pre class=\"codeinput\">project = simulinkproject;\r\nprojectRoot = project.RootFolder;\r\n\r\nmyCacheFolder = fullfile(projectRoot, <span class=\"string\">'work'<\/span>);\r\nmyCodeFolder = fullfile(projectRoot, <span class=\"string\">'code'<\/span>);\r\n\r\nSimulink.fileGenControl(<span class=\"string\">'set'<\/span>,<span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'CacheFolder'<\/span>, myCacheFolder,<span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'CodeGenFolder'<\/span>, myCodeFolder,<span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'createDir'<\/span>, true)\r\n<\/pre>\r\n\r\n<p>That way, all the code and MEX-files generated by the model will go in those folders and the current directory will remain clean.<\/p>\r\n\r\n<p>Once you are done working on this project, you can restore the values stored in the Simulink Preferences using this line:<\/p>\r\n\r\n<pre class=\"codeinput\">Simulink.fileGenControl(<span class=\"string\">'reset'<\/span>)\r\n<\/pre>\r\n\r\n<p><strong>Don't Clean Up: Faster Initialization<\/strong><\/p>\r\n\r\n<p>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.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Let us know how you use this feature by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=3863&#comment\">comment here<\/a><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q2\/simulinkPrefs.png\" onError=\"this.style.display ='none';\" \/><\/div><p>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... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2015\/01\/16\/controlling-the-location-of-the-generated-code-and-temporary-files\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,33],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3863"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=3863"}],"version-history":[{"count":20,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3863\/revisions"}],"predecessor-version":[{"id":8570,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/3863\/revisions\/8570"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=3863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=3863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=3863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}