MATLAB Answers

How can I link a data dictionary to a Simulink block library?

56 views (last 30 days)
William McHargue
William McHargue on 22 May 2015
Commented: William McHargue on 24 May 2015
I've created a Simulink library with blocks that use an enumerated type ("LU_Modes") that I've defined in my main model's data dictionary. In a library block I can create a constant with an "Output data type:" of class "Enum: LU_Modes" without Simulink complaining when I apply the setting.
However, I cannot set the constant's value to "LU_Modes.Off" (where "Off" is one of the enumeration values defined) without an error stating:
"The model uses enumerated type 'LU_Modes' defined in design_data.sldd, but the model is not linked to that dictionary."
I've tried using simply "Off" instead of "LU_Modes.Off" for the constant value but, while it will accept it, when I update or build the model Simulink complains "Off" is an undefined function or variable.
Logically, I would like my library to be linked to my data dictionary, but I've looked everywhere I can think of to configure that and cannot find where. In a model's properties there is a "Data" tab that allows for its linking to a data dictionary, but no such tab in a library's properties.
How can one create blocks within a library that use values from a data dictionary? Would using referenced models be the answer?
Thank you for any help in this regard,
Bill.

Answers (1)

Sebastian Castro
Sebastian Castro on 22 May 2015
Edited: Sebastian Castro on 22 May 2015
That's a tricky one... I can think of one solution that could potentially work, but would require R2015a since this is where the Simulink Data Dictionary API came out.
I'd say something along the lines of:
  • Create a block callback in the library block that triggers when you add that library block to your model (CopyFcn callback, maybe?)
  • This function will look at your current model's data dictionary hierarchy and see whether you already have that enumeration defined in there.
  • If you don't have the necessary enumeration, the function will add some "canned" dictionary you provide as part of the data dictionary reference hierarchy.
To do this, I right-clicked the library block and selected "Properties". In the Block Callbacks tab, I added in the following code under CopyFcn:
ddName = get_param(bdroot,'DataDictionary');
dd = Simulink.data.dictionary.open(ddName);
data = getSection(dd,'Design Data');
if ~exist(data,'MyEnum')
addDataSource(dd,'LibDictionary.sldd');
saveChanges(dd);
end
This worked fine for me -- what do you think?
- Sebastian
  1 Comment
William McHargue
William McHargue on 24 May 2015
Sebastian,
Thanks for the very quick reply and suggestion. We are using R2015a so I'll be able to experiment with your suggestion. I was and am hopeful there is a workaround that simply involves an assignment or linking of the library to the data dictionary, but right now I have to work on the main code and I'll have to leave the ability to use my enumeration out of the library block. :-(
Your answer has made me aware of the API and I look forward to investigating that and playing with your CopyFcn suggestion.
Bill.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!