How to create custom library in simulink : tutorial 8




In this tutorial, I will explain you how to create a library in Simulink. Up till know we have been working with the library browser of simulink. Whenever we open the library browser there a number of blocks in the library browser. Each of these block does a specific task and that task is designed and defined by the programmer. In this tutorial we will design a block similar to that present in the library browser. The purpose of this tutorial is solely to make u familiarize with how to create that block. Examples included will not do any specific task it will be just an input output block and I will explain you how to design a simple block.




How to create custom library in simulink

  • Open MATLAB and then open Simulink as we have been doing in all previous tutorial. But instead of creating new model as we have been doing up till now we will create new library as we are interesting in creating a new library of our own. Click on the blank library block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 1: Blank library

  • On this new blank library click on the library browser icon. As I have mentioned earlier I am not going to design a library for a specific purpose. It will be just a general purpose library which will receive an input and return as it is at the output. Therefore, from the library browser select the commonly used block section and then select the input block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 2: Input block

  • Add this block on the created library. From the same section of library browser select the output block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 3: Output block




  • Add this block to the library you created and place it in front of the input block. Connect both the blocks with each other as shown in the figure belowHow to create custom library in simulink : tutorial 8

Figure 4: Diagram

  • Select the complete block diagram and right click on the selection a dropdown menu will appear and from that menu select Create subsystem from selection as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 5: Creating subsystem

  • You can also create the subsystem by pressing the short key Ctrl +G. After clicking on subsystem the complete block diagram will be converted to a block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 6: Subsystem

  • After the subsystem is created you can remove the input and output blocks and the input output ports will be there to provide input and get the output as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 7: Ports

  • When you double click on the subsystem or the block created then inside the subsystem there will be the block diagram we created previously as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 8: Inside subsystem

  • We can change the format of the block or the subsystem we created by clicking right on the block. We can change the colors and styles of the block. Right click on the block and then click on format and then click on foreground color. A list will appear, from that list select the color of your choice as shown in the figure belowHow to create custom library in simulink : tutorial 8

Figure 9: Foreground color

  • Again do the same for the background color by right clicking on the block and then click on format and then click on background color. A list will appear, from that list select the color of your choice as shown in the figure belowHow to create custom library in simulink : tutorial 8

Figure 10: Background color

  • Now we want to do the masking of our created library, or block you can say. Right click on the created block and from the drop down menu select mask a sub menu will appear from that sub menu select Create mask as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 11: Creating mask

  • The created mask will look like the one shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 12: Mask

  • Anything you write in this icon drawing command will appear on the block we created. For instance, if we want the name of the block to appear on the block we write it here in a disp command and that will appear on the block itself. Now if I write the command given in the figure below,How to create custom library in simulink : tutorial 8

Figure 13: Block name

  • And click on apply and then ok, this name “my_library” will appear on the center of the block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 14: Name of the block

  • We can also create an input label of the block by writing the command given in the figure below,How to create custom library in simulink : tutorial 8

Figure 15: Input label

  • The input label will appear at the input port of the block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 16: Input port label

  • Same can be done for the output port. By writing a command shown in the figure below on the icon drawing command we can write on the output portHow to create custom library in simulink : tutorial 8

Figure 17: Output label

  • This will display a label at the output port of the block as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 18: Output port label

  • We can also write in the help menu of the block using this mask editor. Go to the documentation section of the mask edition as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 19: Documentation

  • In the help section of documentation, whatever you write will appear in the help web of the block. For example, I write in the help section “This is the help of my_library” as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 20: Help command

  • Now go back to the simulink main page, write click on the block you created, and from the dropdown menu select the help button as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 21: Help of block

  • This will open the help browser of the block and in that help browser will appear the command we entered previously in the help section as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 22: Help browser

  • Now comes the part of testing the library we created. Click on file of the simulink window then click on new and then on blank model as we will test the library in the model as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 23: Blank model

  • In the blank model drag and drop the block from the library window you created previously. After that right click on the block placed then click on mask and then on look under mask as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 24: Look under mask

  • You can also do this by pressing a shortcut key Ctrl+U The block diagram under the mask is shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 25: Diagram under mask

  • You can only edit this diagram in the library folder and not in the model. And if we change the diagram in the library we created as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 26: Updated diagram in library

  • This will also be update the diagram in the model as shown in the figure below,How to create custom library in simulink : tutorial 8

Figure 27: Updated model

However, this library cannot be updated in the model.

Exercise:

  • Create a library block with 2 inputs and 1 output. The inputs will take two integers and at the output the sum of the integers is returned



2 thoughts on “How to create custom library in simulink : tutorial 8”

    • you are welcome. please check our other tutorial on simulink also and keep visiting our website for more information related to simulink.

Leave a Comment