Documentation

Add Libraries to the Library Browser

This example shows how to create a block library and add it to the Simulink® Library Browser. You also add an annotation to the library that appears in the Library Browser.

You create a function slblocks to specify information about your library. You can save the function as a .m or .mlx file. You cannot save it as a P-code file.

  1. From the Simulink start page, select Blank Library and click Create Library.

  2. Add a Gain block and two Subsystem blocks to the library. Name the Subsystem blocks Subsystem1 and Subsystem2. In the Subsystem2 properties, set the OpenFcn callback to mylib2.

  3. At the MATLAB® command prompt, enter this command to enable the model property EnableLBRepository. Your library can appear in the browser only if this property is on 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. Create another library mylib2 and add some blocks and an annotation that contains the text My annotation. Right-click the annotation and select Show in Library Browser.

  6. Add a description for the annotation. The description appears in the Library Browser when you hover over the annotation. You can do this programmatically using set_param. Use find_system to get the annotation handle. Use part of your annotation text with the 'Name' argument. The 'RegExp' argument enables partial matches.

    ann = find_system('mylib2','findall','on','RegExp','on','Type','annotation',...
    'Name','annotation');
    set_param(ann,'Description','Use this annotation to label the model.');
  7. Save mylib2 to the same folder you saved mylib to.

    You can close both libraries if you want.

  8. In MATLAB, right-click the folder you saved the library to and select New File > Script. Name the file slblocks.m.

  9. Open slblocks.m. Add this function to it and save.

    function blkStruct = slblocks
    		% This function specifies that the library should appear
    		% in the Library Browser
    		% and be cached in the browser repository
    
    		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; 

  10. In the Library Browser, refresh to see the new library. Right-click the library list and select Refresh Library Browser.

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

    Because of the callback you created, clicking Subsystem2 displays the contents of the mylib2 library. Hovering over the annotation in the Library Browser displays the description.

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

    Click Fix and respond to the prompt as appropriate.

Specify Library Order in the 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, at the command prompt, enter:

sl_refresh_customizations

Related Examples

More About

Was this topic helpful?