Add Libraries to Library Browser and Quick Insert
This example shows how to add a library to the Simulink® Library Browser and how to configure a library block to add it to the quick insert menu.
Add Library to Library Browser
The Library Browser displays a tree view of the available libraries on your system that you can browse to add blocks to your models.
To add a custom library to the Library Browser, use these steps:
1. Load and unlock an existing library myLib
. You can also create a new library. For more information, see Create Custom Library.
load_system("myLib") set_param("myLib","Lock","off")
2. To display annotations in the Library Browser, add text or image annotations in the library, right-click them and select Show in Library Browser. For information on how to set annotations in a model, see Annotate Models.
You can arrange the blocks and annotations in your library in any order. By default, they appear alphabetically in the Library Browser, with subsystems listed first, followed by blocks, and then annotations. Users of your library can use the Library Browser context menu to switch between alphabetical order and your specified order. When users choose your specified order, the arrangement in your library determines how they appear on the grid in the Library Browser.
3. In the MATLAB® Command Window, enter this command to set the EnableLBRepository
library property to on
.
set_param("myLib","EnableLBRepository","on");
Your library will appear in the Library Browser only if this property is enabled at the time of saving your library.
4. Save the library in a folder on the MATLAB path or add its location to the MATLAB path.
save_system("myLib");
5. To specify how your custom library should appear in the Library Browser, use these steps:
Create a new script
slblocks
in the folder that contains your custom library.Add this function to specify that the custom library
myLib
will appear with the nameMy Library
in the Library Browser:
function blkStruct = slblocks Browser.Library = "myLib"; Browser.Name = "My Library"; blkStruct.Browser = Browser;
Save
slblocks
. You can save the file as either a.m
or.mlx
file, but not as a P-code file.
6. To open the Library Browser, click Library Browser in the Simulink Toolstrip .
7. To view the new library in the Library Browser, right-click the library list in the Library Browser and select Refresh Library Browser.
Note, if you save a custom library without setting EnableLBRepository
to on, a message will appear at the top of the Library Browser. Click Fix and respond to the prompt as appropriate.
Add Sublibrary to Library
If your library contains several blocks, you can improve its organization by grouping related blocks in separate Subsystem blocks to create sublibraries.
To create a sublibrary in the library myLib
, use these steps:
Add a Subsystem block named
Basic Signals
.Inside
Basic Signals
, delete the default Inport and Outport blocks. If the input and output ports of all the blocks inside a Subsystem block are connected, the Subsystem block is considered a reusable library block and not a referenced library.Add Step, Ramp, and Sin blocks inside
Basic Signals
block.Optionally, you can create a mask for the Subsystem block to display text or an image that conveys the purpose of the library. However, the mask can only have an icon. If you add code or parameters in the Subsystem block mask, the Subsystem block will be considered a reusable library block and not a referenced library.
Save
myLib
.To view the sublibrary in the Library Browser, right-click the library list and select Refresh Library Browser.
Considerations While Using Sublibraries
If the name of the Subsystem block contains the word Examples, capitalized as shown, the Library Browser will display the subsystem as a block rather than as a sublibrary.
If you create a subsystem hierarchy in a library, you must use unique subsystem names. For example, do not create a hierarchy such as
SubsystemA/SubsystemB/SubsystemA
.
Set Library Order in Library List
You can arrange the order in which your library appears relative to others in the Library Browser. By default, the Simulink library appears at the top of the list with a sort priority of -1. Other libraries have a sort priority of 0 and appear below the Simulink library. To place your library at the top of the list, set its sort priority to -2. Libraries with the same sort priority are sorted alphabetically.
To set the sort priority of a library, add a sl_customization.m
file to the MATLAB path. This sample content of sl_customization
places the library My Library
at the top of the list of libraries:
function sl_customization(cm) cm.LibraryBrowserCustomizer.applyOrder({'My Library',-2}); end
To apply the customization immediately, use this command in the MATLAB Command window:
sl_refresh_customizations
Add Library Blocks to Quick Insert Menu
You can add keywords to a block in your library and use the keywords to insert library blocks in a model from the quick insert menu. The quick insert menu enables you to add blocks directly to a model without leaving the canvas. You just need to click the canvas to open the quick insert menu and start typing the keyword or block name to add the blocks.
Use these steps to add keywords to a block in a custom library:
1. Using the set_param
function, set the BlockKeywords
parameter of Gain block :
set_param('myLib/Gain','BlockKeywords',["My Gain", "Your Gain"])
2. Save the library.
save_system("myLib")
3. Refresh the Library Browser to update the quick insert menu with the new keywords.
4. Now, you can add the Gain block to your model by typing the block name or any of the keywords in the quick insert menu.
Considerations While Using Quick Insert
The supported special characters in keywords are '&', '(', ')', '+', '@', and '!'.
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, you must add in the library where the block is defined.
Quick insert menu supports searching for blocks using keywords in languages other than English by using internationalized keywords.