Create a Custom Library

Create a Library

You can create your own library and, optionally, add it to the Simulink® Library Browser. You save a library as a .SLX file as you do a model. However, you cannot simulate in a library, and a library becomes locked for editing each time you close it. You must unlock a library before you make changes to it. See Lock and Unlock Libraries.

  1. From the Simulink start page, select Blank Library and click Create Library.

  2. Add blocks from models or libraries to the new library. Make the changes you want to the blocks, such as changing block parameters, adding masks, or adding blocks to subsystems.

    Subsystem names in a library hierarchy must be unique. For example, do not create a hierarchy such as Subsystem_Name1/Subsystem_Name2/Subsystem_Name1.

  3. Add annotations or images. Right-click the ones you want to appear in the library in the Library Browser and select Show in Library Browser.

  4. If you plan to add the library to the Library Browser, you can order the blocks and annotations in your library model. By default, they appear alphabetically in the Library Browser, with subsystems first, then blocks, and then annotations. The user of your library can use the Library Browser context menu to choose between viewing them in alphabetical order or the order you specified. When the user selects this option, the order in which they appear in your library model determines the order they appear on the grid in the library in the Library Browser.

  5. If you want the library to appear in the Library Browser, enable the model property EnableLBRepository before you save the library.


  6. Save the library.

    Where you save the library depends on how you plan to use it. If you want to add it to the Library Browser, save it to a folder on the MATLAB® path or add the location to the MATLAB path. Otherwise, save it to a location where the models that use the blocks can access it.

If you want the library to appear in the Library Browser, you must also create a function slblocks on your MATLAB path that adds the library to the browser. For an example that shows complete steps for adding a library to the browser, see Add Libraries to the Library Browser.

Blocks for Custom Libraries

Your library can contain the blocks you need, configured for your purposes. Subsystems, masked blocks, and charts in your library become linked blocks as instances in the model and stay updated if you change them in your library. Knowing about custom blocks is also useful when you create a library. See Types of Custom Blocks.

You can create blocks in custom libraries with settings for specific purposes.

Create a Sublibrary

If your library contains many blocks, you can group the blocks into subsystems or separate sublibraries. To create a sublibrary, you create a library of the sublibrary blocks and reference the library from a Subsystem block in the parent library.

  1. In the library you want to add a sublibrary to, add a Subsystem block.

  2. Inside the Subsystem block, delete the default input and output ports.

  3. If you want, create a mask for the subsystem that displays text or an image that conveys the sublibrary's purpose.

  4. In the subsystem's block properties, set the OpenFcn callback to the name of the library you want to reference.

To learn more about masks, see Create a Simple Mask.

Prevent Library Block from Linking to Instance

You can configure a library block so the instances created from it are not linked blocks and are instead copies. Set the block's CopyFcn callback.


Include Block Description in Linked Block

To add a description that appears in the linked block, mask the library block and add the description in the Documentation pane of the mask. Descriptions added to the library block through the block's properties do not appear on the linked block.

Configure Subsystems with OpenFcn Callback for Library Browser

A common use of a Subsystem block in a custom library is to set the OpenFcn callback property to open a library, creating a library hierarchy. However, you can use the OpenFcn callback property of a Subsystem block for other purposes, for example to run MATLAB code or to open a link.

If your subsystem block in a library is empty and its OpenFcn callback contains code that performs an action other than point to a library or model, then you need to add a 'ShowInLibBrowser' mask parameter to the subsystem to have it appear in the Library Browser.

  1. Right-click the subsystem and select Mask > Create Mask. If the block already has a mask, select Edit Mask instead.

  2. In the Mask Editor Parameters & Dialog tab, on the Controls pane, click Check box.

  3. In the Dialog box pane, set the prompt and name for the new check box to ShowInLibBrowser and click OK.

Annotations in Custom Libraries

You can add annotations in your custom library and optionally have them appear in the Library Browser. For example, you can add an annotation that documents the library. You can also add annotations that the user of your library can add to their model from the Library Browser. Annotations can contain text and images or display an equation. Annotations can also perform an action when clicked. Learn more about annotations in Describe Models Using Annotations.

You can add callout lines from annotations to blocks in your library. However, the callouts do not appear in the Library Browser.

If you want the annotation to appear in the Library Browser, after you add it to your library, right-click it and select Show in Library Browser. If you want a description to appear in a tooltip when the user hovers over the annotation in the Library Browser, add the description to the annotation programmatically. At the MATLAB command prompt, enter:


To get the annotation handle, use find_system. This example gets all the annotations in the library mylib:

ann = find_system('mylib','FindAll','on','Type','annotation');

To get a specific annotation, turn on regular expression search and specify part of the annotation text with the 'Name' argument:

ann = find_system('mylib2',FindAll','on','RegExp','on','Type','annotation',...

Add Libraries to the Library Browser includes instructions for adding an annotation that appears in the Library Browser.

Lock and Unlock Libraries

When you close a library, it becomes locked for editing. When you next open it, unlock it if you want to make changes to it. Click the lock badge in the lower-left corner of the library to unlock it. Additionally, if you try to modify a locked library, a message prompts you to unlock it.

You can unlock a library programmatically. At the MATLAB command prompt, enter:


To lock the library programmatically, enter:


Prevent Disabling of Library Links

By default, a user of the blocks in your library can disable the link to library blocks. If you want to control editing of linked blocks and prevent the block user from disabling links, you can lock links to your library. Locking library links prevents the user from making any changes to the block instances.

  • In your library, select Diagram > Lock Links To Library.

To understand how the block user interacts with blocks from locked libraries, see Lock Links to Blocks in a Library.

More About

Was this topic helpful?