{"id":4743,"date":"2015-08-20T01:00:15","date_gmt":"2015-08-20T06:00:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=4743"},"modified":"2015-08-11T15:20:55","modified_gmt":"2015-08-11T20:20:55","slug":"getting-the-value-of-mask-parameters","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2015\/08\/20\/getting-the-value-of-mask-parameters\/","title":{"rendered":"Getting the Value of Mask Parameters"},"content":{"rendered":"<p>Today I want to share a trick I recently found out.<\/p>\r\n\r\n<p><strong>The Problem: Getting the Value of Nested Mask Parameters<\/strong><\/p>\r\n\r\n<p>I want to write a script that will look in a model for blocks of a particular type, a subsystem I made, and analyze the values passed to it as dialog parameters. I do not want this script to be an <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/mask-code-execution.html\">initialization callback<\/a>, I just want the users to be able to run it whenever they want, like a <a href=\"\">Model Advisor check<\/a>.<\/p>\r\n\r\n<p>When my block is at the top of a model, it is very easy. Let's say I have variables \"a\" and \"b\" defined in the base workspace with respective values of 2 and 3. If the user specifies \"a+b\" as value of parameter \"c\" in my block, I can get the final value of \"c\" using the code below:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q3\/justOneblock.png\" alt=\"Example Masked Subsystem\" \/><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q3\/oneBlockValue.png\" alt=\"Getting the value of a mask parameter\" \/><\/p>\r\n\r\n<p>But what if the user puts my block inside another subsystem:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q3\/maskModel.png\" alt=\"Example Masked Subsystem\" \/><\/p>\r\n\r\n<p>In this case, \"a\" and \"b\" are not in the base workspace anymore, they are in the mask workspace of subsystem SS_Level1.<\/p>\r\n\r\n<p><strong>The Solution<\/strong><\/p>\r\n\r\n<p>If you look carefully at the bottom of the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/mask-parameters.html\">reference page for Mask Parameters<\/a>, you will find one named <tt>maskWSVariables<\/tt><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q3\/maskWSVariables.png\" alt=\"maskWSVariables\" \/><\/p>\r\n\r\nThis parameter will return an array of structures with fields <tt>Name<\/tt> and <tt>Value<\/tt> for each parameter of your mask. Once I have it, I like using a <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/containers.map-class.html (link no longer works)\">containers.Map<\/a> object to access the parameter values easily. For example:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q3\/getMaskValue.png\" alt=\"Getting Mask Parameters\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Let us know what you think of this technique, or if you have preferred tricks to access values and properties in a model by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=4743&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q3\/getMaskValue.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Today I want to share a trick I recently found out.\r\n\r\nThe Problem: Getting the Value of Nested Mask Parameters\r\n\r\nI want to write a script that will look in a model for blocks of a particular type,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2015\/08\/20\/getting-the-value-of-mask-parameters\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,47,69],"tags":[437],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4743"}],"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=4743"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4743\/revisions"}],"predecessor-version":[{"id":4750,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4743\/revisions\/4750"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=4743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=4743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=4743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}