{"id":24,"date":"2008-07-27T23:58:50","date_gmt":"2008-07-28T04:58:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/07\/27\/how-to-make-your-own-simulink-block\/"},"modified":"2016-12-20T17:06:51","modified_gmt":"2016-12-20T22:06:51","slug":"how-to-make-your-own-simulink-block","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/07\/27\/how-to-make-your-own-simulink-block\/","title":{"rendered":"How To Make Your Own Simulink Block"},"content":{"rendered":"<p>Today I want to introduce a fundamental Simulink concept: <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/simulink\/block-masks.html\">masking\r\na block<\/a>.\u00a0 Masking provides you with a way to put an interface on an\r\nalgorithm.\u00a0 This can centralize the system parameters for easier viewing, or hide\r\nthe complexity from unintentional tampering by other users.\u00a0 Masking can also\r\nbe used to dress up your model for more inviting or professional looking\r\npresentations.\u00a0 Let me show you how by masking a subsystem that I made.<\/p>\r\n\r\n<p><strong>The subsystem holds the algorithm<\/strong><\/p>\r\n\r\n<p>In my example, I am masking a subsystem that contains a\r\nfixed limit saturation algorithm:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/saturationAlgorithm.png\" alt=\"Saturdation algorithm subsystem\"><\/p>\r\n\r\n<p>I will come back to this in my examples below.<\/p>\r\n\r\n<p><strong>Improved Presentation: The Mask Icon<\/strong><\/p>\r\n\r\n<p>Simulink models provide an executable specification.\u00a0 The\r\nmodel allows you to share a picture of the system schematics that can be\r\nunderstood by everyone on the team, especially your boss or customer.\u00a0 The best\r\nway to do this is to make the components in your diagram really obvious.\u00a0 A\r\ngreat example of this is the f14_digital demo model.\u00a0 This model uses images for\r\nthe icon on three root level subsystems.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/f14DigitalDiag_large.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/f14DigitalDiag_small.png\" alt=\"f14_digital.mdl demo model\" border=\"0\"><\/a><\/p>\r\n\r\n<p>You can grab a picture of the stuff you are modeling and put\r\nit on the corresponding subsystems.\u00a0 To do this, use the Edit &gt; Mask\r\nSubsystem... menu when the block is selected.\u00a0 This is also available from the right\r\nclick menu on the block.\u00a0 This opens the mask editor and brings up the icon tab\r\nfor the block.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/maskEditor.png\" alt=\"Mask editor GUI, Icon Tab\"><\/p>\r\n\r\n<p>For my Saturation subsystem I have used the basic plot\r\ncommand to put an icon on the block.\u00a0 I set the Units to normalized, which\r\nprovide x and y range from zero to one for the canvas.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/sat_AddIcon.png\" alt=\"Saturation system with added icon\"><\/p>\r\n\r\n<p>The allowed drawing commands are a subset of MATLAB graphics.\u00a0\r\nThere are examples at the bottom of the mask editor.\u00a0 The image command can be\r\ncombined with IMREAD to load an image from your MATLAB path.\u00a0 For example, the\r\nfollowing drawing command adds a Boeing 747 icon to your block<\/p>\r\n\r\n<p><code>image(imread(<span style='color:#A020F0'>'b747.jpg'<\/span>))<\/code><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/mask747.png\" alt=\"Masked subsystem with a Boeing 747 image for the icon\"><\/p>\r\n\r\n<p>Adding an icon to your system doesn\u2019t change the behavior.\u00a0\r\nWhen you double click on the block, you will still open the system it contains.<\/p>\r\n\r\n<p><strong>Mask Dialog and Documentation<\/strong><\/p>\r\n\r\n<p>A mask can also provide a simplified interface to the blocks\r\nunderneath.\u00a0 As a trainer, I used to introduce this concept as a method for\r\npreventing unintended tampering with the contents of your system.\u00a0 The <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/mask-editor-overview.html\">parameters\r\ntab<\/a> of the Mask Editor is where you set the parameters for the system.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/sat_AddParameters.png\" alt=\"Mask Editor: Parameters Tab\"><\/p>\r\n\r\n<p>The Prompt is the string you want to display before the box\r\nwhere the user enters the value.\u00a0 The Variable holds the value entered in the\r\nmask.\u00a0 Adding dialog parameters creates a local workspace for the blocks under\r\nthe mask.\u00a0 In my fixed saturation example, the up and lo constant blocks will\r\nnow use the variables from the mask as their value, instead of hard coding the\r\nconstant in those blocks.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/saturationAlgorithm_Constants.png\" alt=\"Saturation algorithm using variables to specify constant block values\"><\/p>\r\n\r\n<p>Add some details to the documentation\r\ntab to complete the mask dialog.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/sat_AddDocumentation.png\" alt=\"Mask Editor: Documentation Tab\"><\/p>\r\n\r\n<p>Mask type is used to specify a title for the dialog.\u00a0 Mask\r\ndescription shows up right below that and provides a place for some quick\r\nreference documentation.\u00a0 The Mask help field can be a long description that\r\nwill be displayed as HTML when the user pushes the Help button.\u00a0 HTTP links are\r\nalso acceptable.\u00a0 For a full list of the Mask help alternatives <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/simulink\/block-masks.html\">click\r\nhere<\/a>.\u00a0 Double clicking on the icon of the Saturation block displays the finished\r\ndialog:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/satDialog.png\" alt=\"Finished Saturation dialog\"><\/p>\r\n\r\n<p>As you make edits in the Mask Editor you will notice that\r\nyour parameter values in this dialog are set to 0.\u00a0 Before you finish with your\r\nmask, make sure to set the parameter values for your system in the dialog.<\/p>\r\n\r\n<p><strong>How do I get back to the system?<\/strong><\/p>\r\n\r\n<p>Now that the block has a mask, double clicking on the icon\r\nopens the mask dialog.\u00a0 The way you know you are looking at a mask dialog is\r\nthe label following the system name: <em>Saturation (mask)<\/em>.\u00a0 To see the\r\nalgorithm beneath the mask, right click on the block and select <em>Look Under\r\nMask<\/em>.\u00a0 This will bring you back to the contents of the system.<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>This is the tip of the masking iceberg.\u00a0 Do you have masked\r\nblocks in your model?\u00a0 What tricks have you used to polish up your model and\r\nmake it look professional?\u00a0 Tell me about it <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=24&amp;#comment\">here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Today I want to introduce a fundamental Simulink concept: masking\r\na block.\u00a0 Masking provides you with a way to put an interface on an\r\nalgorithm.\u00a0 This can centralize the system parameters for... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/07\/27\/how-to-make-your-own-simulink-block\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,47,30],"tags":[48,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/24"}],"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=24"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":6124,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/24\/revisions\/6124"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}