Documentation

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. If you want the library to appear in the library browser, enable the model property EnableLBRepository before you save the library.

    set_param(gcs,'EnableLBRepository','on');

  4. 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.

set_param(gcbh,'LinkStatus','none'); 

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.

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 dialog box prompts you to unlock it.

You can unlock a library programmatically.

set_param('library_name','Lock','off');

If you unlocked the library programmatically, you must lock it again from the MATLAB command prompt.

set_param('library_name','Lock','on');

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.

Related Examples

More About

Was this topic helpful?