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.
From the Simulink start page, select Blank Library and click Create Library.
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
.
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.
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.
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');
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.
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 list to include the blocks in custom libraries currently in effect. The quick insert list lets you add blocks to a model without leaving the canvas. Click the canvas and start typing to add blocks from the quick insert list.
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.
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.
In the library you want to add a sublibrary to, add a Subsystem block.
Inside the Subsystem block, delete the default input and output ports.
If you want, create a mask for the subsystem that displays text or an image that conveys the sublibrary purpose.
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.
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');
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.
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 prompt 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
prompt.
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 also supports searching for blocks in languages other than English by using internationalized keywords.
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.
Right-click the subsystem and select Mask > Create Mask. If the block already has a mask, select Edit Mask instead.
In the Mask Editor Parameters & Dialog tab, on the Controls pane, click Check box.
In the Dialog box pane, set the prompt and name
for the new check box to ShowInLibBrowser
and click
OK.
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 Notes and 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:
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 the Library Browser includes instructions for adding an annotation that appears in the Library Browser.
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');
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.