How to Create Custom Library in Simulink: Tutorial 8

In this tutorial, we will explain how to create a library in Simulink. Up until now, we have been working with the library browser in Simulink. Whenever we open the library browser, there are a number of blocks to choose from. Each of these blocks 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 familiarize ourselves with how to create that block. Examples included will not do any specific task; they will just be simple input and output blocks, and we will explain how to design a simple block.

A custom library streamlines the modeling process via reusable components and subsystems for specific project requirements. Using a custom library is the best approach for constructing and managing tools to optimize and enhance our design workflows in Simulink.

Some examples of custom libraries are the control system library to store PID controllers, state-space controllers, and other control blocks. Signal processing library for applications such as audio processing, image processing, and data filtering via custom filters, FFT blocks, and various other signal processing algorithms.

Whether we’re designing control systems, signal processing algorithms, or any other complex engineering system, creating a custom library will be the key to achieving greater efficiency and consistency in our Simulink projects.

Open MATLAB and then Simulink, as we have been doing in all previous tutorials. But instead of creating a new model, we will create a new library. Click on the blank library block, as shown in the figure below.

Blank library
Blank library

Placing Components

On this new blank library, click on the library browser icon. As we mentioned earlier, we are not going to design a library for a specific purpose. It will be just a general-purpose library that will receive an input and show it on 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.

Input block
Input block

Add this block to the newly created library. From the same section of the library browser, select the output block as shown in the figure below.

Output block
Output block

Add this block to the library we created and place it in front of the input block. Connect both blocks with each other as shown in the figure below.

Diagram of model in library
Diagram

Creating Subsystem

Select the complete block diagram and right-click on the selection. A drop-down menu will appear, and from that menu, select Create subsystem from selection. Refer to the figure below.

Creating subsystem in library
Creating subsystem

We can also create the subsystem by pressing the short key Ctrl +G. After clicking on the subsystem, the complete block diagram will be converted to a block, as shown in the figure below.

Subsystem
Subsystem

After the subsystem is created, we can remove the input and output blocks. The input and output ports will be there to provide input and get output, as shown in the figure below.

Ports
Ports

When we double-click on the subsystem or newly created block inside the subsystem, as shown in the figure below.

Inside subsystem
Inside subsystem

Formatting Block

We can change the format of the block or the subsystem we created by right-clicking on the block. We can also change the colors and styles of blocks in the same way. Right-click on the block, then click on format, and then click on foreground color. A list will appear; from that list, we can select the color of our choice, as shown in the figure below.

Foreground color
Foreground color

Again, do the same for the background color by right-clicking on the block, clicking on format, and then clicking on background color. A list will appear; from that list, we can select the color of our choice, as shown in the figure below.

Background color
Background color

Now we want to mask our newly created library, or block. Right-click on the newly created block, and from the drop-down menu, select Mask. A submenu will appear. From that submenu, select Create Mask, as shown in the figure below.

Creating mask in library
Creating mask

The mask created will look like the one shown in the figure below.

Mask
Mask

Block Labels

Anything we 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, we write the command as shown in the figure below.

Block name
Block name

And click on Apply, and then OK. This name, “my_library”, will appear in the center of the block, as shown in the figure below.

Name of the block of library
Name of the block

We can also create an input label for the block by writing the command given in the figure below.

Input label
Input label

The input label will appear at the input port of the block, as shown in the figure below.

Figure 16: Input port label

The same can be done for the output port. By writing the command shown in the figure below on the icon drawing command, we can write it on the output port.

Output label
Output label

This will display a label at the output port of the block, as shown in the figure below.

Output port label as my_library
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.

Documentation
Documentation

Help Command

In the help section of the documentation, whatever we write will appear in the help web of the block. For example, we write in the help section, “This is the help of my_library”, as shown in the figure below.

Help command
Help command

Now go back to the Simulink main page, right-click on the block we created, and from the dropdown menu, select the help button as shown in the figure below.

Help of block in library
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.

Help browser of library
Help browser

Now comes the part about testing the library we created. Click on file in the Simulink window, then click on new, and then on blank model, as we will test the library in the model. Refer to the figure below.

Blank model
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.

Look under mask
Look under mask

Updating Block Diagram

We can also do this by pressing the shortcut key Ctrl+U. The block diagram under the mask is shown in the figure below.

Diagram under mask of library
Diagram under mask

We can only edit this diagram in the library folder and not in the model. We can change the diagram in the library we created, as shown in the figure below.

Updated diagram in library
Updated diagram in library

This will also update the diagram in the model, as we can see in the figure below.

Updated model of custom library
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.

Conclusion

In conclusion, this tutorial provides an in-depth overview of designing custom libraries for Simulink. It covers step-by-step procedures along with an example of explanation to help us better understand the concept. You can utilize this to develop more custom blocks and libraries according to your requirements. At last, we have provided an exercise to reinforce the concept of this tutorial. Hopefully, this was helpful in expanding your knowledge of Simulink.

You may also like to read:

This concludes today’s article. If you face any issues or difficulties, let us know in the comment section below.

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.

      Reply

Leave a Comment