{"id":5066,"date":"2016-02-02T19:55:24","date_gmt":"2016-02-03T00:55:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=5066"},"modified":"2016-02-02T19:55:24","modified_gmt":"2016-02-03T00:55:24","slug":"the-variables-you-need","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2016\/02\/02\/the-variables-you-need\/","title":{"rendered":"The variables you need"},"content":{"rendered":"<p>Today I want to share two lines of code I find very useful.<\/p>\r\n\r\n<p><strong>The problem<\/strong><\/p>\r\n\r\n<p>I often need to exchange models with users. One of the thing people struggle the most while doing that is providing the appropriate data needed by the model. Usually, I see two cases:<\/p>\r\n\r\n<ul>\r\n\t<li>The user saves his entire MATLAB workspace in a MAT-file and sends that with the model. In such case, I often receive a very large MAT-file where only a few variables are actually needed.<\/li>\r\n\r\n\t<li>The user tries to optimize and sends only the minimum set of variables, but forgets a few. This leads to an error like:<\/li>\r\n<\/ul>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q1\/undefinedVaraibles.png\" alt=\"Undefined variable\" \/><\/p>\r\n\r\n<p><strong>The Solution<\/strong><\/p>\r\n\r\n<p>In an ideal world, my data would be generated by a script or stored in a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-a-data-dictionary.html\">data dictionary<\/a> and all the dependencies would be taken care of by a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/simulink-project-file-management.html\">Simulink Project<\/a>.<\/p>\r\n\r\n<p>However, for me it often happens that I need to share a model which is in a \"debugging\" state. For example, I have deleted half the blocks, modified data in the base workspace, etc. In that case, I want to store the data used by the model that is currently in the base workspace, without all the unnecessary variables.<\/p>\r\n\r\n<p>In my opinion, the easiest way to make that happen is the following two lines of code:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q1\/findVars.png\" alt=\"Simulink.findVars\" \/><\/p>\r\n\r\n<p>With <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.findvars.html\">Simulink.findVars<\/a>, you get a list of all the variables in the base workspace used by the model, and with <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.io.savevariablestoscript.html\">matlab.io.saveVariablesToScript<\/a>, you generate a script that can recreate them.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q1\/createdFile.png\" alt=\"File storing variables\" \/><\/p>\r\n\r\n<p>Then if I want to be super-efficient, I will copy the content of that file to the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/model-callbacks.html\">model preLoad callback<\/a>. That way I only need to send one file, and the data will be created automatically the next time the model is opened.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q1\/mldelCalback.png\" alt=\"preload Callback\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Give this technique a try next time you need to share a model; hopefully it will save you some time.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q1\/mldelCalback.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Today I want to share two lines of code I find very useful.\r\n\r\nThe problem\r\n\r\nI often need to exchange models with users. One of the thing people struggle the most while doing that is providing the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/02\/02\/the-variables-you-need\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,69,33],"tags":[131,464],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5066"}],"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=5066"}],"version-history":[{"count":11,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5066\/revisions"}],"predecessor-version":[{"id":5077,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5066\/revisions\/5077"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=5066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=5066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=5066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}