{"id":5911,"date":"2016-09-08T18:26:14","date_gmt":"2016-09-08T23:26:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=5911"},"modified":"2016-09-08T18:26:14","modified_gmt":"2016-09-08T23:26:14","slug":"including-a-mask-image-in-your-block","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2016\/09\/08\/including-a-mask-image-in-your-block\/","title":{"rendered":"Including a mask image in your block"},"content":{"rendered":"<p>Earlier today, a colleague came to me asking for a way to include an image in a block to be used as mask image. I though it might be interesting to share my response here.<\/p>\n<p><strong>The Problem<\/strong><\/p>\n<p>Here is the question I received:<\/p>\n<p><em>I want to mask a block and display an image on the mask. The image is stored in a .PNG file. When I will distribute the block, I would prefer sharing only a Simulink file, and not the image.<\/em><\/p>\n<p><strong>The Solution<\/strong><\/p>\n<p>As <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/create-mask-icon.html\">described here<\/a>, it is possible to use the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/image.html\">image<\/a> function to read and display an image file on a block mask. However, with this technique the image must be on the MATLAB path.<\/p>\n<p>To avoid the need to carry the image file, it is possible to <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/associating-user-data-with-blocks.html\">associate the image data with the block<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q3\/setUserData.png\" alt=\"Associating image data with blocks\" \/><\/p>\n<p>It is important to explicitly make the <tt>UserData<\/tt> persistent, otherwise it will not be saved with the model. I also like to make the <tt>UserData<\/tt> a structure. That way if someone else wants to save data there, it will be possible to create a new field.<\/p>\n<p>Once this is done, the image data will stay with the block. You can copy it to a different subsystem or to a new model, it will remain associated with the block.<\/p>\n<p>Once the data is associated with the block, you can retrieve it in the mask icon drawing commands, and display it:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q3\/maskIcon.png\" alt=\"Displaying the Image\" \/><\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>How are you displaying images in your masked subsystems? Let us know by leaving a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q3\/maskIcon.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Earlier today, a colleague came to me asking for a way to include an image in a block to be used as mask image. I though it might be interesting to share my response here.<br \/>\nThe Problem<br \/>\nHere is the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/09\/08\/including-a-mask-image-in-your-block\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[47],"tags":[484],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5911"}],"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=5911"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5911\/revisions"}],"predecessor-version":[{"id":5920,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5911\/revisions\/5920"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=5911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=5911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=5911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}