Main Content

Link 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:

  1. Create a new data dictionary or open an existing dictionary. To create a new dictionary, in the Model Explorer, selectFile>New>Data Dictionary. To open an existing dictionary, selectFile>Openand browse for your dictionary.

  2. Add data and types, such as bus objects, to the data dictionary.

    模型浏览器。在左边,一个数据dictionary node is expanded in the Model Hierarchy pane. On the right, the Contents pane displays the two bus objects contained in the Design Data section of the dictionary.

  3. Save the data dictionary.

You can attach the data dictionary to your library by using the UI or programmatically by using theset_paramfunction.

通过使用附加的数据字典UI:

  1. Unlock your library (seeLock and Unlock Libraries).

  2. In the Simulink Editor, on theModelingtab, clickLibrary Properties.

  3. In the Library Properties dialog box, on theExternal Datatab, clickBrowseto locate and open your data dictionary.

  4. ClickOKto attach the dictionary.

通过使用附加的数据字典set_paramfunction, use this command.

set_param('myLib','DataDictionary','myLibData.sldd');

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.

模型浏览器。On the left, the library node is expanded in the Model Hierarchy pane. Under the library node, the External Data node is expanded to show the attached data dictionary. On the right, the Contents pane displays the two bus objects contained in the Design Data section of the dictionary.

As you develop blocks for your library, you can select the types contained in the library dictionary from theData typelist.

On the left, the model canvas displays the contents of a subsystem library block. The Out block is selected. On the right, a block parameters dialog box displays the dictionary data types in the drop-down menu for Data type.

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 badgein the bottom left corner, then clickExternal Data. The dictionary appears as an external data source to your model under theFrom Librariesnode.

模型浏览器。On the left, a model node is expanded in the Model Hierarchy pane. Under the model node, the External Data node is expanded. Under the External Data node, the From Libraries node is expanded to show the attached data dictionary. On the right, the Contents pane displays the two bus objects contained in the Design Data section of the dictionary.

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.

On the left, the model canvas displays a library block used in a model. The Out block is selected. On the right, a block parameters dialog box displays the dictionary data types in the drop-down menu for Data type.

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 theFrom Librariesnode.

Considerations When Using a Library Dictionary

There are several considerations when authoring or using a library with a linked 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 link your model to the library dictionary. Once the link to the library dictionary is restored, access to the data is also restored.

  • If you are unable to relink your library to a library dictionary, the cache that contains information about library dictionary links might be corrupted. To reset the cache, call theSimulink.LibraryDictionary.resetLibraryLinksfunction. After calling this function, the ability to link your library to a library dictionary is restored, but information on previously linked 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 theSimulink.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 theSimulink.LibraryDictionary.clearfunction.

  • 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.

  • 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-linked data dictionary.

See Also

|||

Related Topics