Main Content

Create Custom Library

You can create your own library and, optionally, add it to the Simulink® Library Browser. You save a library as a .slx file similar to the way you save 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.

    Note

    The Library Browser only supports libraries. You cannot display the contents of a model in the Library Browser.

  2. (Optional) Define data types to be used on block interfaces in a Simulink data dictionary. Then, attach the data dictionary to the library. Library users automatically gain access to the data contained in the dictionary when they drag a block from the library into their model.

  3. Add blocks 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.

  4. (Optional) 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.

  5. If you plan to add the library to the Library Browser, you can order the blocks and annotations in your library. 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 determines the order they appear on the grid in the library in the Library Browser.

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

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

  7. 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 Library Browser.

Note

To update the Library Browser with your custom libraries, right-click anywhere in the Library Browser library list and select Refresh Library Browser. Refreshing the Library Browser also updates the quick insert menu to include the blocks in custom libraries currently in effect. The quick insert menu lets you add blocks to a model without leaving the canvas. Click the canvas and start typing to add blocks from the quick insert menu.

Data Dictionary for Custom Libraries

When you define data types — such as bus and enumeration types — to be used on block interfaces, you can make these data types available to users by storing them in a data dictionary that is attached to the library. When a user drags a block from the library into their model, the model automatically gains access to the data contained in the dictionary.

  1. Create a data dictionary.

  2. In the dictionary, define bus objects, enumeration types, or other data objects that you will use for the interface definitions of your library blocks.

  3. Attach the dictionary as an external data source for your library programmatically by using set_param or in the UI by using the External Data tab in the Library Properties dialog box. The Model Explorer displays the attached dictionary and its content as an external data source for the library.

  4. To save the data dictionary specifications, save the library.

The types that you defined in the library are now available to you while developing library blocks. See Link Data Dictionary to Custom Libraries.

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 Design and Create a Custom Block.

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

  4. In the subsystem 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 Block with Keywords for Quick Insert

You can add one or more keywords to a block in your library. The keyword lets you add the block to your model from the quick insert menu by entering the keyword or the block name.

For example, suppose you have a custom Gain block in your library. You can add the keyword My Gain to the block. Then, you can add the block to your model by entering My Gain at the quick insert menu.

In the Quick Insert menu, the word "my" is entered, and in the search results, My Gain is selected.

Note

You cannot add keywords to the blocks from Commonly Used Blocks as they are a place to view frequently used blocks from various libraries. If you want to add keywords to a block available in Commonly Used Blocks, make sure that you set it in the library where the block is defined.

To add the keyword to the block in your library, use set_param with the 'BlockKeywords' parameter. You can use a character vector, string scalar, or string array as the value. For example:

set_param(gcb,'BlockKeywords',{"My Gain","Your Gain"})
set_param(gcb,'BlockKeywords','My Integrator')

Note

The supported special characters in keywords are '&', '(', ')', '+', '@', '!'.

Note

The quick insert menu also supports searching for blocks in languages other than English by using internationalized keywords.

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 a Subsystem block in a library is empty and its OpenFcn callback contains code that performs an action other than point to a library, 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.

    In the Mask Editor for the subsystem, ShowInLibBrowser is selected.

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 Annotate Models.

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:

set_param(annotationHandle,'Description','descriptionText')

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','Name','matchingText');

Add Libraries to 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:

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

To lock the library programmatically, enter:

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, on the Library tab, click Lock Links.

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

Related Topics