{"id":475,"date":"2010-10-12T18:47:41","date_gmt":"2010-10-12T18:47:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/2010\/10\/12\/writing-out-your-workspace-variables-as-a-script-with-simulinksavevars\/"},"modified":"2010-10-12T18:47:41","modified_gmt":"2010-10-12T18:47:41","slug":"writing-out-your-workspace-variables-as-a-script-with-simulinksavevars","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2010\/10\/12\/writing-out-your-workspace-variables-as-a-script-with-simulinksavevars\/","title":{"rendered":"Writing out your workspace variables as a script with Simulink.saveVars"},"content":{"rendered":"I almost never use Simulink, so I never saw this function before (<strong>requires Simulink<\/strong>).\r\n\r\nTry this at the command line:\r\n<code> <\/code>\r\n<pre>\r\na = 1;\r\nb = 2.5;\r\nc = 'A string';\r\nd = {a, b, c};\r\n\r\nSimulink.saveVars('MyVars');<\/pre>\r\nIt produces a file like this:\r\n\r\n<code> <\/code>\r\n<pre>\r\n% -------------------------------------------------------------------\r\n%  MATLAB file generated by Simulink.saveVars on 27-Sep-2010 13:52:16\r\n%  MATLAB version: 7.11.0.584 (R2010b)\r\n% -------------------------------------------------------------------\r\n\r\na = 1;\r\n\r\nb = 2.5;\r\n\r\nc = 'A string';\r\n\r\nd = cell(1, 3);\r\nd{1} = 1;\r\nd{2} = 2.5;\r\nd{3} = 'A string';<\/pre>\r\n<hr>\r\n<p>\r\nI can see something like this being a lot more transparent than saving variables to a .mat file. for small variables.  For example, if you want to take your current workspace and use it as the basis for a script.  This script can be modified easily, and you can even call saveVars again to write new or updated data into your modified script.\r\n<p>\r\n\r\nIt&#8217;s quite configurable.\u00a0 For instance, you can specify the size threshold above which large variables will just be saved to a .MAT file instead of written to the script (the script automatically loads the .MAT file). You can even control the maximum width of the generated code.\r\n<p>\r\n\r\nYou might find that it will gives me a more readable script than simply loading a .mat file.  People reading your code will see more of what you are doing.  This helps with readability.\r\n<p>\r\n\r\nWhat kinds of uses can you think of for this cool function?","protected":false},"excerpt":{"rendered":"<p>I almost never use Simulink, so I never saw this function before (requires Simulink).\r\n\r\nTry this at the command line:\r\n \r\n\r\na = 1;\r\nb = 2.5;\r\nc = &#8216;A string&#8217;;\r\nd = {a, b,&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2010\/10\/12\/writing-out-your-workspace-variables-as-a-script-with-simulinksavevars\/\">read more >><\/a><\/p>","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/475"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}