{"id":46,"date":"2009-02-15T15:12:12","date_gmt":"2009-02-15T15:12:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/02\/15\/where-does-that-variable-come-from\/"},"modified":"2009-02-19T17:32:25","modified_gmt":"2009-02-19T17:32:25","slug":"where-does-that-variable-come-from","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/02\/15\/where-does-that-variable-come-from\/","title":{"rendered":"Where Does That Variable Come From?"},"content":{"rendered":"<p>Most Simulink models use workspace variable to define the\r\nimportant parameters.\u00a0 For example, setting the gain value to K and then\r\ndefining K in the workspace.\u00a0 I wrote about <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/2008\/12\/25\/initializing-parameters\/\">initializing\r\nthese parameters in the base workspace<\/a> in a recent post.\u00a0 Have you ever\r\nnoticed that there are many sources for these parameters?\u00a0 The value of K might\r\nbe in the base workspace, or it could be in the model workspace.\u00a0 The source of\r\nK could even be in a mask.<\/p>\r\n\r\n<p>Simulink determines the value of a variable by searching in\r\nthe workspaces above the block that uses the variable. \u00a0Starting from the\r\nblock, it searches each mask workspace up the subsystem hierarchy until it reaches\r\nthe root of the model.\u00a0 Simulink searches the model workspace and the base\r\nworkspace last, respectively.\u00a0 This process is <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/toolbox\/simulink\/ug\/brjlptu.html#brjnb77-1_1\">hierarchical\r\nsymbol resolution<\/a>, and in this post, I will demonstrate these rules that determine\r\nhow Simulink resolves the value of a variable.<\/p>\r\n\r\n<p><strong>An Example Model<\/strong><\/p>\r\n\r\n<p>To illustrate the concepts of hierarchical symbol\r\nresolution, let\u2019s use a simple model (<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22989-hierarchical-symbol-resolution\">Download here<\/a>).\u00a0 This model consists of nothing but subsystems,\r\nconstant blocks and displays.\u00a0 The constant blocks refer to variables in\r\ndifferent workspaces and get their value based on where they are in the\r\nhierarchy.\u00a0 Starting at the root of the model, we find the variables k and m.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/rootVariableResolution.png\" alt=\"Variable Resolution Simulink model.\"><\/p>\r\n\r\n<p><strong>Look in the Workspace above the Block<\/strong><\/p>\r\n\r\n<p>I think of the model workspace as being at the root of the\r\nmodel.\u00a0 The model workspace is the first place to search for k and m.\u00a0 I use\r\nthe Model Explorer to see the contents of the model workspace.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/modelExplorerModelWS.png\" alt=\"Model explorer showing the model workspace.\"><\/p>\r\n\r\n<p>We have found k, but m is not in the model workspace.\u00a0 The\r\nnext workspace up in the hierarchy is the base workspace.\u00a0 The model explorer\r\nconveniently shows a tree that reflects the model hierarchy.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/modelExplorerBaseWS.png\" alt=\"Model explorer showing the base workspace.\"><\/p>\r\n\r\n<p><strong>Mask Workspaces<\/strong><\/p>\r\n\r\n<p>Masked subsystems introduce mask workspaces in the model hierarchy.\u00a0\r\nIf the block is in a masked subsystem, it first evaluates in the mask workspace\r\nthen works its way up through the hierarchy until it reaches the root of the\r\nmodel.\u00a0 Here is the Top subsystem.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/topSubsystem.png\" alt=\"Top subsystem, top subsystem mask and the contents of the system.\"><\/p>\r\n\r\n<p>The mask dialog initializes the variable m with a value of\r\n3000.\u00a0 The variable k is also appears at this level of the model, but resolves\r\nto the variable defined at the model workspace level.<\/p>\r\n\r\n<p><strong>Subsystems Inherit Variables from Their Context<\/strong><\/p>\r\n\r\n<p>Because of hierarchical symbol resolution, subsystems\r\ninherit variables from their context.\u00a0 The Bottom subsystem refers to k, which\r\ncomes from its mask.\u00a0 It also refers to m, which is resolved in the Top Mask.\u00a0\r\nIf the Bottom subsystem were at the root of the model, m would resolve to the\r\nbase workspace.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/bottomSubsystem.png\" alt=\"Bottom subsystem, bottom subsystem mask and the contents of the system.\"><\/p>\r\n\r\n<p>This behavior can make the same library blocks behave\r\ndifferently depending on their parent system.\u00a0 For example, a pendulum\r\nsubsystem may inherit the acceleration due to gravity (g) from its parent system.\u00a0\r\nThese parent system may define variables for different environments.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/pendulumsInheritGravity.png\" alt=\"A subsystem hierarchy that would allow inheritance of the variable g based on context.\"><\/p>\r\n\r\n<p><strong>Controlling Hierarchical Resolution<\/strong><\/p>\r\n\r\n<p>You can control the resolution of variables in your subsystem\r\nby setting the Permit hierarchical resolution parameter in the subsystem\r\nparameters (right click on the subsystem and select Subsystem parameters\u2026). <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/subsystemParametersHierarchicalResolution.png\" alt=\"Subsystem parameters, Permit hierarchical resolution.\"><\/p>\r\n\r\n<p>The default setting is <em>All<\/em> and gives you the behavior\r\ndescribed above.\u00a0 If you don\u2019t want variables to resolve to symbols defined in\r\nthe hierarchy above your subsystem you can change this to <em>None<\/em>.\u00a0 \u00a0The <em>ExplicitOnly<\/em>\r\nsetting controls resolution of signal and state names by only resolving objects\r\nthat have been set to resolve explicitly.<\/p>\r\n\r\n<p><strong>What about Model Reference Hierarchy?<\/strong><\/p>\r\n\r\n<p>Hierarchical symbol resolution stops when it gets to the\r\nroot of the model.\u00a0 Model reference hierarchy is not considered as part of\r\nhierarchical symbol resolution.\u00a0 Any model must be able to define itself stand\r\nalone, or as part of a larger model reference hierarchy.\u00a0 It is possible to\r\npass variables down into a model workspace through the model reference arguments.\u00a0\r\nFor more on that, see documentation about <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/toolbox\/simulink\/ug\/f4-141787.html#f4-141806\">using\r\nmodel arguments<\/a>.<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>If you program in M-code than you might see discussion as similar\r\nto the way variables are resolved in nested functions of an M-file.\u00a0 Do you\r\ntake advantage of hierarchical symbol resolution?\u00a0 Leave a <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=46&amp;#comment\">comment here<\/a> and\r\ntell us how.<\/p>","protected":false},"excerpt":{"rendered":"<p>Most Simulink models use workspace variable to define the\r\nimportant parameters.\u00a0 For example, setting the gain value to K and then\r\ndefining K in the workspace.\u00a0 I wrote about initializing\r\nthese... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/02\/15\/where-does-that-variable-come-from\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[47,69],"tags":[78,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/46"}],"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=46"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}