Attach Data Dictionary to Custom Libraries
When you create a custom block library, you might define data objects — such as bus and enumeration types — to be used on block interfaces. You can make these data types available to library users by storing them in a Simulink® data dictionary that is attached to the library. Library users automatically gain access to the data types contained in the dictionary when they drag a block from the library into their model.
Author Library Blocks with Data Types Defined in Attached Data Dictionary
To define data and types for your library blocks in a data dictionary:
Create a new data dictionary or open an existing dictionary. To create a new dictionary, in the Model Explorer, select File > New > Data Dictionary. To open an existing dictionary, select File > Open and browse for your dictionary.
Add data and types, such as bus objects, to the data dictionary.
Save the data dictionary.
You can attach the data dictionary to your library by using the UI or
programmatically by using the
To attach the data dictionary by using the UI:
Unlock your library (see Lock and Unlock Libraries).
In the Simulink Editor, on the Modeling tab, click Library Properties.
In the Library Properties dialog box, on the External Data tab, click Browse to locate and open your data dictionary.
Click OK to attach the dictionary.
To attach the data dictionary by using the
function, use this
After you have attached the dictionary and saved your library, the dictionary and its content appears in the Model Explorer as an external data source for the library.
As you develop blocks for your library, you can select the types contained in the library dictionary from the Data type list.
Use Blocks From a Library with Attached Data Dictionary
When you drag a block from a library into your model, the model automatically gains access to data objects contained in the dictionary attached to that library. To see the dictionary in the Model Explorer, in the Simulink Editor, click the model data badge in the bottom left corner, then click External Data. The dictionary appears as an external data source to your model under the From Libraries node.
The data objects defined in the library are now available to the model for simulation and code generation without the need to bring those data objects into either the base workspace or another data dictionary.
When you update or save your model, if the model no longer contains a block from the library, the library dictionary is no longer available to your model and the dictionary is removed from the From Libraries node.
Considerations When Using a Library Dictionary
There are several considerations when authoring or using a library with an attached data dictionary.
In most cases, you will define bus or enumeration types in a library dictionary, but library authors can share any type of object with library users by adding objects to the dictionary.
Items in the library dictionary are global in scope, and symbol resolution occurs as if the library dictionary is directly connected to the model.
If a library link is broken, the data associated with the library is no longer available. To regain access to the data, you must manually attach the data dictionary to your model. Once the dictionary is attached, access to the data is also restored.
If you are unable to manually attach data dictionary to your library, the cache that contains information about library dictionary links might be corrupted. To reset the cache, call the
Simulink.LibraryDictionary.resetLibraryLinksfunction. After calling this function, the ability to attach a data dictionary to your library is restored, but information on previously attached library dictionaries is lost.
In a model hierarchy, a model must directly or indirectly reference a library dictionary to use the data objects that the dictionary contains.
Simulink stores library dictionary dependency information in the user preference folder. This information is persistent across MATLAB® sessions. If you have moved a library to a different folder or machine, or if you have made changes to a library hierarchy, you can use the
Simulink.LibraryDictionary.refreshfunction to perform a dependency analysis and update your stored dependencies. This dependency analysis can be expensive, so call the refresh function only if you have a missing dependency. To clear the previous dependency analysis, use the
When you drag a block from a library into your model, the model gains access to the data dictionary attached to that library. However, if the block is from a sublibrary, access is only to the data dictionary attached to that sublibrary. In this case, to provide a model with access to both library and sublibrary dictionaries when it uses the block, the library author must contain the sublibrary block in a new subsystem.
When you add a library block to a subsystem file, the data dictionary attached to the library becomes available to the subsystem file. If you refer to the subsystem file from a model using a Subsystem Reference block, the library dictionary is available within the Subsystem Reference block boundary only.
If you store your library under the MATLAB root folder, you can attach a dictionary to the library, but a model does not have automatic access to the dictionary when you add a block from the library.
Simulink does not support finding unused variables for a library with an attached data dictionary.