| Simulink® | ![]() |
| On this page… |
|---|
You can create your own block library and add it to the Simulink Library Browser (see Adding Libraries to the Library Browser).
Tip If your library contains many blocks, consider grouping the blocks into a hierarchy of sublibraries (see Creating a Sublibrary). |
To create a library:
Select Library from the New submenu of the File menu.
Simulink creates a model (*.mdl) file for storing the new library and displays the file in a new model editor window.
Tip You can also use the new_system command to create the library and the open_system command to open the new library. |
Drag blocks from models or other libraries into the new library.
Note If you want to be able to create links in models to a block in the library, you must provide a mask (see Creating Block Masks) for the block. You can also provide a mask for a subsystem in a library but you do not need to do so in order to create links to it in models. |
Save the library's model file under a new name.
Creating a sublibrary entails inserting a reference in the model (.mdl) file of one library to the model file of another library. The referenced file is called a sublibrary of the parent (i.e., referencing) library. The sublibrary is said to be included by reference in the parent library.
To include a library in another library as a sublibrary:
Open the parent library.
Unlock the parent library (see Modifying a Library ).
Add a Subsystem block to the parent library.
Delete the subsystem's default input and output ports.
Create a mask for the subsystem that displays text or an image that conveys the sublibrary's purpose.
Set the subsystem's OpenFcn parameter to the name of the sublibrary's model file.
Save the parent library.
When you open a library, it is automatically locked and you cannot modify its contents. To unlock the library, select Unlock Library from the Edit menu. Closing the library window locks the library.
To lock a block library, save and close the library or set its Lock parameter to 'on' at the MATLAB command line, using the set_param command. Locking a library prevents a user from inadvertently modifying a library, for example, by moving a block in the library or adding or deleting a block from the library. If you attempt to modify a locked library, Simulink displays a dialog box that allows you to unlock the library and make the change. You must then relock the library from the MATLAB command line to prevent further changes.
Simulink provides the following features to facilitate making changes to library blocks without invalidating models that use the library blocks.
Library forwarding tables enable Simulink to update models to reflect changes in the names or locations of the library blocks that they reference. For example, suppose that you rename a block in a library. You can use a forwarding table for that library to enable Simulink to update models that reference the block under its old name to reference it under its new name.
Simulink allows you to associate a forwarding table with any library. The forwarding table for a library specifies the old locations and new locations of blocks that have moved within the library or to another library. You associate a forwarding table with a library by setting its ForwardingTable parameter to a cell array of two-element cell arrays, each of which specifies the old and new path of a block that has moved. For example, the following command creates a forwarding table and assigns it to a library named Lib1.
set_param('Lib1', 'ForwardingTable', {{'Lib1/A', 'Lib2/A'}
{'Lib1/B', 'Lib1/C'}});
The forwarding table specifies that block A has moved from Lib1 to Lib2. and that block B is now named C. Suppose that you open a model that contains links to Lib1/A and Lib1/B. Simulink updates the link to Lib1/A to refer to Lib2/A and the link to Lib1/B to refer to Lib1/C. The changes become permanent when you subsequently save the model.
Simulink lets you create aliases, i.e., alternate names, for a mask's parameters. A model can then refer to the mask parameter by either its name or its alias. This allows you to change the name of a mask parameter in a library block without having to recreate links to the block in existing models (see Example: Using Mask Parameter Aliases to Create Backward-Compatible Parameter Name Changes).
To create aliases for a masked block's mask parameters, use the set_param command to set the block's MaskVarAliases parameter to a cell array that specifies the names of the aliases in the same order as the mask names appear in the block's MaskVariables parameter.
Example: Using Mask Parameter Aliases to Create Backward-Compatible Parameter Name Changes. The following example illustrates the use of mask parameter aliases to create backward-compatible parameter name changes.
Create a library named mylib.
Create the masked subsystem described in Masked Subsystem Example in mylib.
Name the masked subsystem Line.
Set the masked subsystem's annotation property (see Block Annotation Pane ) to display the value of its m and b parameters, i.e., to
m = %<m> b = %<b>
The library appears as follows:

Save mylib.
Create a model named mymdl.
Create an instance of the Line block in mymdl.
Rename the instance LineA.
Change the value of LineA's m parameter to -1.5.
Change the value of LineA's b parameter to 3.
Set LineA's annotation property to display the values of its m and b parameters.

Configure mymdl to use a fixed-step, discrete solver with a step size of 0.1 second.
Save mymdl.
Simulate mymdl.
Note that the model simulates without error.
Close mymdl.
Unlock mylib.
Rename the m parameter of the Line block in mylib to slope.
Rename Line's b parameter to intercept.
Change Line's mask icon and annotation properties to reflect the parameter name changes.

Save mylib.
Reopen mymdl.

Note that LineA's icon has reverted to the appearance of its library master (i.e., mylib/Line) and that its annotation displays question marks for the values of m and b. These changes reflect the parameter name changes in the library block . In particular, Simulink cannot find any parameters named m and b in the library block and hence does not know what to do with the instance values for those parameters. As a result, LineA reverts to the default values for the slope and intercept parameters, thereby inadvertently changing the behavior of the model. The following steps show how to use parameter aliases to avoid this inadvertent change of behavior.
Close mymdl.
Unlock mylib.
Select the Line block in mylib.
Execute the following command at the MATLAB command line.
set_param(gcb, 'MaskVarAliases',{'m', 'b'})This specifies that m and b are aliases for the Line block's slope and intercept parameters.
Reopen mymdl.

Note that LineA's appearance now reflects the value of the slope parameter under its original name, i.e., m. This is because when Simulink opened the model, it found that m is an alias for slope and assigned the value of m stored in the model file to LineA's slope parameter.
Change LineA's block annotation property to reflect LineA's parameter name changes, i.e., replace
m = %<m> b = %<b>
with
m = %<slope> b = %<intercept>
LineA now appears as follows.

Note that LineA's annotation shows that, thanks to parameter aliasing, Simulink has correctly applied the parameter values stored for LineA in mymdl's model file to the block's renamed parameters.
![]() | Browsing and Searching Block Libraries | Adding Libraries to the Library Browser | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |