Including a mask image in your block
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.
Here is the question I received:
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.
As described here, it is possible to use the image function to read and display an image file on a block mask. However, with this technique the image must be on the MATLAB path.
To avoid the need to carry the image file, it is possible to associate the image data with the block.
It is important to explicitly make the UserData persistent, otherwise it will not be saved with the model. I also like to make the UserData a structure. That way if someone else wants to save data there, it will be possible to create a new field.
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.
Once the data is associated with the block, you can retrieve it in the mask icon drawing commands, and display it:
Now it's your turn
How are you displaying images in your masked subsystems? Let us know by leaving a comment below.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.