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.slxfile 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. SeeLock and Unlock Libraries.

  1. From the Simulink start page, selectBlank Libraryand clickCreate 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 asSubsystem_Name1/Subsystem_Name2/Subsystem_Name1.

  4. Add annotations or images. Right-click the ones you want to appear in the library in the Library Browser and selectShow in Library Browser.

  5. 我f 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. 我f you want the library to appear in the Library Browser, enable theEnableLBRepositorylibrary 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.

我f you want the library to appear in the Library Browser, you must also create a functionslblockson your MATLAB path that adds the library to the browser. For an example that shows complete steps for adding a library to the browser, seeAdd Libraries to Library Browser.

Note

To update the Library Browser with your custom libraries, right-click anywhere in the Library Browser library list and selectRefresh 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. 我n 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 usingset_paramor in the UI by using theExternal Datatab 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. SeeAttach 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. SeeDesign and Create a Custom Block.

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

创建子程序库

我f 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 aSubsystemblock in the parent library.

  1. 我n the library you want to add a sublibrary to, add aSubsystemblock.

  2. 我nside theSubsystemblock, delete the default input and output ports.

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

  4. 我n the subsystem block properties, set theOpenFcncallback to the name of the library you want to reference.

To learn more about masks, seeCreate 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’sCopyFcncallback.

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

我nclude Block Description in Linked Block

To add a description that appears in the linked block, mask the library block and add the description in theDocumentationpane 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 customGainblock in your library. You can add the keywordMy Gainto the block. Then, you can add the block to your model by enteringMy Gainat the quick insert menu.

Note

You cannot add keywords to the blocks fromCommonly Used Blocksas they are a place to view frequently used blocks from various libraries. If you want to add keywords to a block available inCommonly 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, useset_paramwith 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 aSubsystemblock in a custom library is to set theOpenFcn回调房地产开放图书馆,创建一个brary hierarchy. However, you can use theOpenFcncallback property of aSubsystemblock for other purposes, for example to run MATLAB code or to open a link.

我f aSubsystemblock in a library is empty and itsOpenFcncallback 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 selectMask>Create Mask. If the block already has a mask, selectEdit Maskinstead.

  2. 我n the Mask EditorParameters & Dialogtab, on theControlspane, clickCheck box.

  3. 我n theDialog boxpane, set the prompt and name for the new check box toShowInLibBrowserand clickOK.

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

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

我f you want the annotation to appear in the Library Browser, after you add it to your library, right-click it and selectShow 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, usefind_system. This example gets all the annotations in the librarymylib:

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 Browserincludes 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');

防止禁用图书馆的链接

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.

  • 我n your library, on theLibrarytab, clickLock Links.

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

Related Topics