{"id":89,"date":"2010-04-22T20:55:40","date_gmt":"2010-04-22T20:55:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2010\/04\/22\/variables-you-depend-on\/"},"modified":"2017-01-02T16:09:39","modified_gmt":"2017-01-02T21:09:39","slug":"variables-you-depend-on","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2010\/04\/22\/variables-you-depend-on\/","title":{"rendered":"Variables You Depend On"},"content":{"rendered":"<style type=\"text\/css\">\r\n\/* Publisher style MATLAB code *\/ \r\n\r\npre.codeinput {\r\n  background: #EEEEEE;\r\n  padding: 10px;\r\n}\r\n@media print {\r\n  pre.codeinput {word-wrap:break-word; width:100%;}\r\n} \r\n\r\nspan.keyword {color: #0000FF}\r\nspan.comment {color: #228B22}\r\nspan.string {color: #A020F0}\r\nspan.untermstring {color: #B20000}\r\nspan.syscmd {color: #B28C00}\r\n\r\npre.codeoutput {\r\n  color: #666666;\r\n  padding: 10px;\r\n}\r\n\r\npre.error {\r\n  color: red;\r\n}\r\n<\/style>\r\n\r\n<p>Have you ever sent someone a Simulink model only to find out they couldn't run it because you didn't include the required variables? MathWorks technical support has this problem on a daily basis. This week guest blogger  \r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31762\">Parasar Kodati<\/a> \r\nshares with us a new feature to address this issue called <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/simulink.findvars.html\">Simulink.findVars<\/a><\/tt>.<\/p>\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/Parasar_Kodati.png\" alt=\"Simulink variable finder, Parasar Kodati\"\/>\r\n\r\n<p><strong>A New Feature for Variable Management<\/strong><\/p>\r\n\r\n<p>We all use variables in our models. Variables help us identify the \r\nparameters that determine the behavior of the model and vary them as \r\nneeded. I am sure you would agree that it is very easy to introduce \r\nvariables in a model but difficult to find and keep track of them. One \r\ncommon issue I have seen as a Tech-Support engineer is variables defined \r\nwith the same name at multiple places in a model. It is also hard to know \r\nwhere in a model a variable is. The  <tt>Simulink.findVars<\/tt> command \r\nmakes it very easy to identify where a model uses a given variable and in \r\nwhich workspace it is stored. Check out the video\r\n  I recorded to demonstrate this feature. In this post, I will show you \r\n  how to use the <tt>Simulink.findVars<\/tt> command with the well-known demo model, <tt>sf_car<\/tt>:<\/p> \r\n\r\n  <div class=\"content\">\r\n<pre class=\"codeinput\">\r\nfind_vehicledata = Simulink.findVars(<span class=\"string\">'sf_car'<\/span>,<span class=\"string\">'Name'<\/span>,<span class=\"string\">'vehicledata'<\/span>)\r\n<\/pre>\r\n<pre class=\"codeoutput\">\r\nfind_vehicledata = \r\n\r\n  Simulink.WorkspaceVar handle\r\n  Package: Simulink\r\n\r\n  Properties:\r\n             Name: 'vehicledata'\r\n        Workspace: 'sf_car'\r\n    WorkspaceType: 'model'\r\n     UsedByBlocks: {'sf_car\/Vehicle'}\r\n<\/pre>\r\n\r\n<p>The Properties section of the above output shows that the model uses \r\nthe variable <tt>vehicledata<\/tt>, it is used by the block \r\n<tt>sf_car\/Vehicle<\/tt>, and stored in the model workspace \r\n(see the <tt>WorkspaceType<\/tt> property). This is why if you try to \r\nchange the variable <tt>vehicledata<\/tt> in the base workspace, \r\nit has no effect. Also, note that the return object \r\n<tt>find_vehicledata<\/tt> is of type <tt>Simulink.WorkspaceVar<\/tt>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/findVars_600_2.png\" alt=\"Simulink uses variables from the base, model and mask workspace, Simulink.findVars can identify where a variable is from.\"><\/p>\r\n\r\n<p>Another scenario is when you have many variables in your MATLAB base \r\nworkspace and you want to know which ones you need to simulate a model. \r\nInstead of using the <tt>Name<\/tt> property this time, you can use \r\nthe <tt>WorkspaceType<\/tt> property as follows:<\/p>\r\n\r\n<pre class=\"codeinput\"> base_vars = Simulink.findVars(<span class=\"string\">'sf_car'<\/span>,<span class=\"string\">'WorkspaceType'<\/span>,<span class=\"string\">'base'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nbase_vars = \r\n\r\n  0x0 empty Simulink.WorkspaceVar handle\r\n  Package: Simulink\r\n\r\n  Properties:\r\n    Name\r\n    Workspace\r\n    WorkspaceType\r\n    UsedByBlocks\r\n<\/pre>\r\n<p>This time the return object is empty indicating that the model is not \r\nusing any of the base workspace variables. I hope that \r\n<tt>Simulink.findVars<\/tt> provides you a way to manage your \r\nworkspaces and variables.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/simulink.findvars.html\">Simulink.findVars documentation<\/a> \r\nincludes more options and use-cases. Try out <tt>Simulink.findVars<\/tt> \r\non your models and let us know what you find with a \r\n<a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=89&#comment\">comment here<\/a>.<\/p> \r\n","protected":false},"excerpt":{"rendered":"<p>\r\n\/* Publisher style MATLAB code *\/ \r\n\r\npre.codeinput {\r\n  background: #EEEEEE;\r\n  padding: 10px;\r\n}\r\n@media print {\r\n  pre.codeinput {word-wrap:break-word; width:100%;}\r\n} \r\n\r\nspan.keyword {color:... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2010\/04\/22\/variables-you-depend-on\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,65,24,69,33,16],"tags":[131,441,132],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/89"}],"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=89"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":6193,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/89\/revisions\/6193"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}