Guy on Simulink

Simulink & Model-Based Design

Libraries in Simulink

Have you ever noticed the same block constructs occurring repeatedly in your model?  Simulink libraries provide you with a way to capture the template for an algorithm, and then reuse that template all over your model.  For this post, I will to introduce the basic concepts of libraries, library blocks, and library links.

Creating a library

To create a Simulink library, you have to create a new Simulink library from the File> New > Library menu, or using the new_system function.

The New Library Menu

Libraries are special MDL-files.  When you look at a library, you will notice that it does not have the simulation controls a normal model has.  The library is not a functional model; it is a palette of components.  Here is an example library that contains a block for doing variable saturation of a signal.

The saturation library diagram.

The library can be constructed in the same way a model is constructed, by dragging blocks into the library canvas and connecting signals.  The Saturation Dynamic block is just a subsystem that contains the algorithm for dynamic saturation of the signal u:

The Saturation Dynamic algorithm

Locking the library

When a library is first created, it is unlocked.

Unlocked library

This allows you to edit and build the components.  Once you save the library and close it, the library is locked.

Locked library

No changes are allowed without unlocking the library.  This can be done from Edit > Unlock Library.  Most people just drag a block a few pixels to trigger Simulink to prompt you to unlock the library.

Modify locked library message

Reference blocks and algorithm reuse

Just like the blocks from the Simulink libraries, you can add the library blocks to any model you are working on.  There is some terminology that goes along with library blocks.  When you add the library block to your model, you are creating a reference block.

Create a reference block by dragging the library block into the model.

The reference block is an instance of the library block, but the contents are not stored in the model.  A model using the reference block stores only a library link, which holds the path to the library block.

>> get_param('satModel/Saturation Dynamic','ReferenceBlock')
ans =
SatLib/Saturation Dynamic

Set the Format > Library Link Display to User to see which blocks in your model are actually library links.  This setting adds a library link icon to the corner of your block.

Format -> Library Link Display -> User

Adding your library to the browser

At this point, you might be asking how to add the library to the library browser.  The short answer is: >> doc slblocks.  I provided an example of this in the historical tour of the library browser.

What do you do with libraries?

Do you maintain your own libraries?  How many components does a typical library contain?  Do you share your work with other modelers?  Post your library to the file exchange, or leave a comment here.

  • print
  • send email


要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。