Generate Empty bus structures from a data dictionary

My objective is to generate empty bus structures from interfaces defined in a data dictionary. I've tried this a few ways but seem to run into an error each way I've tried.

  1. Item one, ive tried exporting the data dictionary to a file, but the bus objects now created cannot reference the internal enumerations, of the data dictionary which are not exported.
  1. Item two, ive tried exporting the bus objects programmatically and have found i run into a similar problem and dont quite get an independent enumeration reference given the global variable.
  1. Item three, the createMATLABstruct function is used in each of these approaches and didnt seem to suffice.

 Risposta accettata

Umar
Umar il 6 Ott 2025

Hi @Dean D,

The issue you're experiencing occurs because enumeration classes defined in a data dictionary are owned exclusively by that dictionary. When the dictionary closes, it clears its enumeration definitions, breaking references in your exported bus structures. So, the solution around this workaround is keeping the data dictionary open when creating and using bus structures:

dd = Simulink.data.dictionary.open('yourDictionary.sldd');
dDataSection = getSection(dd, 'Design Data');
busEntry = getEntry(dDataSection, 'YourBusName');
busObj = getValue(busEntry);
emptyStruct = Simulink.Bus.createMATLABStruct(busObj);
% Keep dictionary open while using emptyStruct

The dictionary must remain open to maintain enumeration definitions in memory. The other alternative approaches to consider:

If you need standalone files:

1. Export both bus objects AND enumeration class definition files (.m files) together

2.Manually maintain both file sets in your project

If enumerations aren't critical for initialization:

1. Modify bus element data types from enumerations to underlying numeric types (uint8, int32, etc.)

2. Generate structures without enumeration dependencies

Note: This loses type safety

For programmatic workflows:

*Save structures to MAT-files while dictionary is open

*Reopen the dictionary before loading saved structures

Key Points

Enumerations defined in a data dictionary cannot exist independently of that dictionary. Your workflow must account for this dependency - either keep the dictionary loaded or export enumeration definitions separately as .m files alongside your bus objects.

Hope this helps.

References

4 Commenti

Umar,
Thank you for the reply and the suggestion. I tried your first suggestion, and received the following error, 'The first Input must be a valid bus object name, or one or more valid hanldes to block ports.'.
When I ran into this previously, I exported the bus to my workspace, but ran into the issue of referencing the enumeration. I just tried your step above to leave the dictionary open when I use createMATLABStruct, via the bus definition in the workspace, but the function still missed the linkage to the enumeration within the data dictionary.

Hi @Dean DeBastiani,

After reviewing the official MathWorks documentation for Simulink.Bus.createMATLABStruct again, I now understand the issue you are having and hopefully should be able to resolve your issue.

Addressing Comment #1: The Error Message

The error 'The first Input must be a valid bus object name, or one or more valid handles to block ports.' occurred because the function couldn't find your bus object where it was looking. The documentation states: "If you specify a Simulink.Bus object, the Simulink.Bus object must be in the MATLAB base workspace or the data dictionary used by the model."

Addressing Comment #2: The Enumeration Linkage Issue

When you exported the bus to the workspace and kept the dictionary open, the function still couldn't find the enumeration because you didn't tell it where to look for the enumeration definitions.

The Correct Solution

Use the fourth syntax of createMATLABStruct that explicitly passes the data dictionary as the scope parameter:

% Open the data dictionary as an object
dd = Simulink.data.dictionary.open('yourDictionary.sldd');
% Pass the dictionary object as the 4th argument (scope parameter)
emptyStruct = Simulink.Bus.createMATLABStruct('YourBusName', [], [], dd);
% Keep dictionary open while using emptyStruct

Why This Works

The scope parameter (4th argument) tells createMATLABStruct to: 1. Look for the bus object definition in the specified data dictionary 2. Resolve enumeration type references within that same dictionary scope

This is the documented workflow for creating structures from dictionary-based bus objects that reference dictionary-based enumerations.

The Syntax Breakdown

S = Simulink.Bus.createMATLABStruct(busSource, vals, sdims, scope) * busSource: 'YourBusName' (as a string) * vals: [] (empty to use ground values) * sdims: [] (empty for scalar structure) * scope: dd (your data dictionary object)

Try this approach and let me know if it resolves both the bus object resolution error and the enumeration linkage issue.

So, this result in a different error, arguing about the second and third input parameter. See error outlined below:
If the first argument is the name of a bus object, you can provide a valid dimension D=[d_1, ..., d_n] as the third input parameter. The value of the dimension
determines the dimension of the resulting structure. If you use a partial structure in the second input parameter, the dimension P=[p_1, ..., p_m] of the partial
structure must be compatible to D (i.e, m <= n and p_i <= d_i for 1<=i<=m). If the first argument is a cell array of bus object names, you can also provide a cell
array of valid dimensions. If the first argument is not the name of a bus object, you can not provide a dimension.
@Umar, It worked! Followed your last instruction and now the input is this:
emptyStruct = Simulink.Bus.createMATLABStruct('YourBusName', [], 1, dd);
Without the '1', the function doesn't know how many structs to make. If you increase this to say '10', it makes an array of 10 structs.
Thank you for the support!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Manage Design Data in Centro assistenza e File Exchange

Prodotti

Release

R2025b

Richiesto:

il 6 Ott 2025

Commentato:

il 7 Ott 2025

Community Treasure Hunt

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

Start Hunting!

Translated by