{"id":41,"date":"2008-12-25T17:00:12","date_gmt":"2008-12-25T17:00:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/12\/25\/initializing-parameters\/"},"modified":"2008-12-25T17:13:54","modified_gmt":"2008-12-25T17:13:54","slug":"initializing-parameters","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/12\/25\/initializing-parameters\/","title":{"rendered":"Initializing Parameters"},"content":{"rendered":"<p>Simulink models often use parameters from the MATLAB base\r\nworkspace.\u00a0 However, Simulink does not save the MATLAB base workspace with the\r\nmodel.\u00a0 This leads me to the following question: <em>How do you initialize the\r\nworkspace for your simulation?<\/em><\/p>\r\n\r\n<p><strong>Simulink Demos<\/strong><\/p>\r\n\r\n<p>Most Simulink Demos initialize the base workspace before\r\nthey are loaded.\u00a0 Let\u2019s take a look at f14.mdl:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/f14variables.png\" alt=\"Simulink F14 model uses MATLAB expressions in Gain blocks.\"><\/p>\r\n\r\n<p>In the F14 model, you see gain blocks with parameters like Zw,\r\nMw, Mq and 1\/Uo.\u00a0 Simulink evaluates those MATLAB expressions in the base\r\nworkspace during initialization of the simulation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/f14workspace.png\" alt=\"MATLAB Workspace with variables from the F14 demo model.\"><\/p>\r\n\r\n<p>You can register <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/toolbox\/simulink\/ug\/f4-122589.html#f4-93072\">model\r\ncallbacks<\/a> that run when you perform different actions on the model.\u00a0 In\r\nthis example, f14.mdl has a PreLoadFcn that runs a script to initializes the\r\nworkspace.\u00a0 You can see the model callbacks by opening the <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/toolbox\/simulink\/ug\/f4-146273.html#f4-140334\">Model\r\nProperties<\/a> from the File menu.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/f14properties.png\" alt=\"Model Properties dialog box for F14 demo model.\"><\/p>\r\n\r\n<p>The model pre-load function executes before the model is\r\nactually loaded. \u00a0If you only need to initialize a few variables for your\r\nmodel, you can enter the MATLAB expressions directly in the pre-load function\r\ncallback.\u00a0 The F14 model calls the M-file script f14dat.m.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q4\/f14datTorn.png\" alt=\"The F14 demo model initializes the workspace with the f14dat.m script.\"><\/p>\r\n\r\n<p>If I am tweaking variables in my model, I like to change\r\nthem directly in the workspace browser, or at the command line.\u00a0 The pre-load\r\nfunction is a good place for initializing variables because it only runs the\r\nfirst time the model is loaded.\u00a0 This allows you to change the variables for\r\nthe current session without modifying the initialization code.<\/p>\r\n\r\n<p><strong>Other techniques for initialization?<\/strong> <\/p>\r\n\r\n<p>How do you go from the blinking MATLAB cursor to a fully\r\ninitialized environment?\u00a0 Leave a <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=41&amp;#comment\">comment here<\/a> and\r\nshare your model initialization tips.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Simulink models often use parameters from the MATLAB base\r\nworkspace.\u00a0 However, Simulink does not save the MATLAB base workspace with the\r\nmodel.\u00a0 This leads me to the following question: How do you... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/12\/25\/initializing-parameters\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[69,33],"tags":[70,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/41"}],"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\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}