Creating Custom Libraries

Libraries are a convenient location to store blocks that are (or are likely to be) used several times in a model or used in several models.

This tutorial discusses the steps involved in creating a library, and some of the features of a library that make it different from a standard model.

The tutorial is split into the following sections,

  1. Creating a Custom Library.
  2. Using a Custom Library.
  3. Adding a Custom Library to the Library Browser.
  4. Features of a Library.

Other tutorials discussing Simulink and its applications for model based design are available on the Software Tutorials page.

Creating a Custom Library

Libraries are created in a very similar way to a standard model. The first step is to use the Library Browser’s File→New→Library pull-down menu to open a new library. (See Figure 1).

Creating a New Library.

Figure 1: Creating a New Library.

As shown in Figure 2, there are several indicators that a library is being created (as opposed the a standard model). Firstly in the top left corner, where the name of the library appears, the word Library is prepended. And secondly in the lower right hand corner the work Unlocked is written. (The implications of a Locked and Unlocked library are discussed in the section Features of a Library.)

A New Library.

Figure 2: A New Library.

Once a new library has been opened, blocks may be added to it in the same (drag and drop) way as blocks are added to a model. However, blocks within a library are typically not connected by signals. Figure 3 shows an example library where blocks from the standard Simulink libraries have been placed in the new library. (In this tutorial blocks from the standard Simulink libraries have been used for convenience. More typically custom blocks are added to a custom library.)

Blocks Added to a New Library.

Figure 3: Blocks Added to a New Library.

Once the required blocks have been added to the new library it should be closed. This has the effect of locking the library. (The implications of a locked and unlocked library are discussed in the section Features of a Library.)

Using a Custom Library

A custom library is used in an identical way to using any other library. The library needs to be opened to use it. This is achieved in the same way as for a model -- see Opening a New or Existing Model within the Model Building with Simulink tutorial. Once opened, blocks are dragged from a library and placed into a model in the usual way.

Adding a Custom Library to the Library Browser

The Library Broswer is a convenient tool for grouping all available libraries, hence when a custom library is created the developer will usually want to add it to the Library Browser.

Adding a library to the Library Browser is achieved by writing a MATLAB function called slblocks (in a file called slblocks.m). The function slblocks must have a specific format, and slblock.m must be in a directory that is on the MATLAB path.

function blkStruct = slblocks
% Function to add a specific custom library to the Library Browser

% Author: Phil Goddard (phil@goddardconsulting.ca)

% Define how the custom library is displayed in the Library Browser
Browser.Library = 'customlib'; % Name of the .mdl file
Browser.Name    = 'My Custom Library';
Browser.IsFlat  = 1; % Is this library "flat" (i.e. no subsystems)?

% Define how the custom library is displayed in the older style
% "Blocksets and Toolboxes" view.
blkStruct.Name = ['My Custom' sprintf('\n') 'Library'];
blkStruct.OpenFcn = 'customlib'; % Name of the .mdl file
blkStruct.MaskDisplay = '';

% Output the required structure
blkStruct.Browser = Browser;

Figure 4: Example slblocks Function.

Figure 4 gives an example slblocks function. It assumes that the library has been saved in a file called customlib.mdl. There are two somewhat distinct parts to slblocks: the first part specifies what is displayed in the Library Browser; and the second part specifies what is displayed when the library is viewed in an older style Blocksets and Toolboxes view. (The Blocksets and Toolboxes view is rarely used but is included for completeness.)

Once slbocks.m has been written it must be saved, and then the Library Browser must be closed and restarted, for the custom library to be seen in the browser. Figure 5 shows the Library Browser after this has been done, with the custom library being displayed.

Custom Library in Library Browser.

Figure 5: Custom Library in Library Browser.

One slblocks function can add multiple libraries to the Library Browser. For an example of this, and a more detailed description of the code syntax used by slblocks, look at the file that controls how the main Simulink library is displayed in the Library Browser. The following code shows how to do this from the MATLAB command prompt

>> edit([matlabroot,'\toolbox\simulink\blocks\slblocks.m']);

Figure 6: Opening a Template slblocks Function.

Features of a Library

Although libraries have a very similar look and feel to a model they do have some distinct features. These include,

This tutorial has discussed topics related to creating and using custom libraries. Other Simulink tutorials are available on the Software Tutorials page.

Back To Top | Simulink Tutorials