Enumerations in Data Dictionary - MATLAB & Simulink (original) (raw)
A SimulinkĀ® data dictionary permanently stores model data including MATLABĀ® variables, data objects, and data types including enumerated types. Enumeration classes defined in MATLAB by the data dictionary are owned by that dictionary and cannot be cleared by using Simulink.clearIntEnumType. To use an enumeration class defined in a data dictionary, the data dictionary must be open. When you close a data dictionary, the dictionary clears the enumeration classes that it owns. If an instance of an enumeration class exists when you close the dictionary, that enumeration class is not cleared and MATLAB becomes the owner of the class. The definition of the class remains in memory until you clear it by using Simulink.clearIntEnumType. To find the enumeration classes that remain in memory, call Simulink.findIntEnumType. For basic information about data dictionaries, see What Is a Data Dictionary?.
Migrate Enumerated Types into Data Dictionary
This example shows how to migrate enumerated types that are used by a model into a data dictionary.
Import Design Data
- Open a model that uses enumerated types for design data or for blocks in the model.
- In the Simulink Editor, on the Modeling tab, in theDesign section, underRepositories, click .
- In the Model Properties dialog box, clickNew to create a data dictionary.
- Name the data dictionary, save it, and click Apply.
- Click Migrate data.
- Click Migrate in response to the message about copying referenced variables.
- Simulink reports the enumerated types that were not imported into the data dictionary.
- Click OK.
A notification appears in the Simulink Editor, reporting that your model is now linked to the data dictionary.
Import Enumerated Types
Import the definitions of enumerated types only after you import all the design data that were creating using the types. When you import enumerated types to a data dictionary, Simulink disables MATLAB files or P-files that contain the type definitions, causing variables that remain in the MATLAB base workspace to lose their definitions.
- At the MATLAB command prompt, get the names of enumerated types that are used in model blocks.
% Find all variables and enumerated types used in model blocks
usedTypesVars = Simulink.findVars('EnumsReporting','IncludeEnumTypes',true);
% Here, EnumsReporting is the name of the model and
% usedTypesVars is an array of Simulink.VariableUsage objects
% Find indices of enumerated types that are defined by MATLAB files or P-files
enumTypesFile = strcmp({usedTypesVars.SourceType},'MATLAB file');
% Find indices of enumerated types that are defined using the function
% Simulink.defineIntEnumType
enumTypesDynamic = strcmp({usedTypesVars.SourceType},'dynamic class');
% In one array, represent indices of both kinds of enumerated types
enumTypesIndex = enumTypesFile | enumTypesDynamic;
% Use logical indexing to return the names of used enumerated types
enumTypeNames = {usedTypesVars(enumTypesIndex).Name}'
enumTypeNames =
'dEnum1'
'dEnum10'
'dEnum2'
'dEnum3'
'dEnum4'
'dEnum5'
'dEnum6'
'dEnum9' - Open the data dictionary, and represent it with a
Simulink.data.Dictionary
object.
ddConnection = Simulink.data.dictionary.open('myEnumsDD.sldd')
ddConnection =
Dictionary with properties:
DataSources: {0x1 cell}
HasUnsavedChanges: 0
NumberOfEntries: 3 - Use the
importEnumTypes
method to import the enumerated types that are used by blocks in the model. The method saves changes made to the target dictionary, so before you use the method, confirm that unsaved changes are acceptable.
[successfulMigrations, unsuccessfulMigrations] = ...
importEnumTypes(ddConnection,enumTypeNames)
successfulMigrations =
1x6 struct array with fields:
className
renamedFiles
unsuccessfulMigrations =
1x2 struct array with fields:
className
reasons
When enumerated types are imported,importEnumTypes
renames the enumerated class definition file by appending.save
to the file name. For example, if the original enumerated class definition is namedEnum1.m
, Simulink renamed the file asEnum1.m.save
.
The structureunsuccessfulMigrations
reports enumerated types that are not migrated. In this example, two enumerated type instances are defined in the model workspace and can be imported after closing the model. Close the model to import these enumerated types. - Open the dictionary to view the migrated enumerated types.
Manipulate Enumerations in Data Dictionary
These examples show how to operate on existing enumerations in a data dictionary.
- Rename Enumerated Type Definition
- Programmatically Display Enumeration Members
- Rename Enumeration Members
- Delete Enumeration Members
- Change Underlying Value of Enumeration Member
Rename Enumerated Type Definition
- In the data dictionary, create a copy of the enumerated type, and rename the copy instead.
- Find enumeration objects used by your model that are derived from the type with the old name.
- Replace these objects with those derived from the renamed type.
- Delete the type with the old name.
Programmatically Display Enumeration Members
To list the valid values of an enumerated type, use code similar to this example that lists the values from enumerated type myColors
contained in the data dictionaryenumDD.sldd
.
dd = Simulink.data.dictionary.open('enumDD.sldd'); sec = getSection(dd, 'Design Data'); ent = getEntry(sec,'myColors'); e = getValue(ent); e.Enumerals.Name Simulink.data.dictionary.closeAll();
Rename Enumeration Members
Use one of the following approaches.
- Select the enumeration within the dictionary, and rename one or more enumeration members.
- If your model references enumeration members, change these references to match the renamed member.
Delete Enumeration Members
- Find references in your model to an enumeration member you want to delete.
- Replace these references with an alternate member.
- Delete the original member from the enumeration.
Change Underlying Value of Enumeration Member
You can change the values of enumeration members when you represent these values as MATLAB variables or by using Value
field ofSimulink.Parameter
objects.
- Find references in your model to an enumeration member whose value you want to change.
- Make a note of these references.
- Change the value of the enumeration member.
- Manually update references to the enumeration member in your model.
Remove Enumerated Types from Data Dictionary
If you decide that you no longer want to define an enumerated type in a dictionary, follow these steps.
- Manually define the enumerated data type in MATLAB. See Use Enumerated Data in Simulink Models.
- Delete instances of the enumeration class. If there is an instance of the enumeration class in existence when you delete the enumerated data type from the dictionary, the dictionary releases control of the enumeration class, but the class remains in memory as a dynamic enumeration class. You can then find or clear the class by usingSimulink.findIntEnumType and Simulink.clearIntEnumType.
- Delete the enumerated type from the dictionary.
See Also
Simulink.data.dictionary.EnumTypeDefinition