Main Content

Add Libraries to Library Browser

This example shows how to create a block library and add it to the Simulink® Library Browser. This example also shows how to add a sublibrary.

To specify information about your library, you create a function named slblocks.

Add Library to Library Browser

  1. On the Simulink start page, in the search box, type library. Select Blank Library, then click Create Library.

    Note

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

  2. Add a Gain block to the library.

  3. In the MATLAB® Command Window, enter this command to set the EnableLBRepository library property to 'on'. Your library will appear in the Library Browser only if this property is enabled when you save your library.

    set_param(gcs,'EnableLBRepository','on');
  4. Save the library in a folder on the MATLAB path. For this example, name the library mylib.

  5. In the MATLAB Current Folder Browser, open the folder that contains mylib, then, in the MATLAB Toolstrip, click New Script.

  6. In the MATLAB Editor that opens, add this function, which specifies that the library mylib should appear in the Library Browser with the name My Library.

    function blkStruct = slblocks
    % This function specifies that the library 'mylib'
    % should appear in the Library Browser with the 
    % name 'My Library'
    
        Browser.Library = 'mylib';
        % 'mylib' is the name of the library
    
        Browser.Name = 'My Library';
        % 'My Library' is the library name that appears
        % in the Library Browser
    
        blkStruct.Browser = Browser;

  7. Save the function as slblocks.m.

    Note

    You can save the function as an .m or .mlx file. You cannot save the function as a P-code file.

  8. To open the Library Browser, in the Simulink Toolstrip, click Library Browser.

  9. To see the new library in the Library Browser, right-click the library list and select Refresh Library Browser.

    The Library Browser tree shows a library with the name My Library. My Library is expanded in the tree, and beneath the library name, there is a Gain block.

    The figure shows the example library mylib with the Library Browser name My Library.

Note

If you save your library without setting 'EnableLBRepository' to 'on', a message appears at the top of the Library Browser.

At the top of the Library Browser in docked mode, there is a message that says "Some libraries are missing repository information. Fix" The word "Fix" is a hyperlink.

Click Fix and respond to the prompt as appropriate.

Add Sublibrary to Library Browser

Follow these steps to add a sublibrary named mylib2 to your mylib library.

  1. Create a library named mylib2. Add some blocks to it.

  2. In the MATLAB Command Window, set the EnableLBRepository library property to 'on' for the new library.

    set_param(gcs,'EnableLBRepository','on');
  3. Save mylib2 to the folder that contains mylib.

  4. Add a Subsystem block to the mylib library.

  5. Remove the input and output ports from the subsystem by deleting the Inport and Outport blocks inside the subsystem.

  6. Name the Subsystem block My Sublibrary.

  7. To link the Subsystem block to the sublibrary, set the OpenFcn callback to mylib2 by using the Property Inspector.

    The Property Inspector lets you set the OpenFcn callback on the Properties tab.

  8. Save mylib.

  9. To see the new sublibrary in the Library Browser, right-click the library list and select Refresh Library Browser.

    In the Library Browser tree, My Library is expanded. My Library contains a Gain block and a sublibrary called My Sublibrary. My Sublibrary is expanded and contains a Ramp block, a Sine Wave block, and a Step block.

    The figure shows the example sublibrary mylib2 with the Library Browser name My Sublibrary.

    Because of the callback you created, expanding My Sublibrary displays the contents of the mylib2 library.

Specify Library Order in Library List

You can specify the order of your library relative to the other libraries in the list by adding a sl_customization.m file to the MATLAB path and setting the sort priority of your library. For example, to see your library at the top of the list, you can set the sort priority to -2. By default, the sort priority of the Simulink library is -1. The other libraries have a sort priority of 0 by default, and these libraries appear below the Simulink library. Libraries with the same sort priority appear in alphabetical order.

This sample content of the sl_customization.m file places the new library at the top of the list of libraries.

function sl_customization(cm)
% Change the order of libraries in the Simulink Library Browser. 
cm.LibraryBrowserCustomizer.applyOrder({'My Library',-2});
end

To make the customization take effect immediately, in the MATLAB Command Window, enter:

sl_refresh_customizations

My Library appears at the top of the Library Browser tree

Related Topics