Main Content

Enumerations in Data Dictionary

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

  1. Open a model that uses enumerated types for design data or for blocks in the model.

  2. In the Simulink Editor, on the Modeling tab, in the Design section, under Repositories, click Link to Data Dictionary.

  3. In the Model Properties dialog box, click New to create a data dictionary.

  4. Name the data dictionary, save it, and click Apply.

  5. Click Migrate data.

  6. Click Migrate in response to the message about copying referenced variables.

  7. Simulink reports the enumerated types that were not imported into the data dictionary.

    Enumerated Type Migration dialog box with a list of enumerated types that were not imported into the data dictionary

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

  1. 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'
  2. 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
    

  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 named Enum1.m, Simulink renamed the file as Enum1.m.save.

    The structure unsuccessfulMigrations 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.

  4. Open the dictionary to view the migrated enumerated types.

    View of Model Explorer. In the Model Hierarchy pane, the Design Data node of a data dictionary is selected. In the Contents pane, an enumerated type that has been migrated into the data dictionary is selected. The Dialog pane displays the definition of the selected enumerated type.

Manipulate Enumerations in Data Dictionary

These examples show how to operate on existing enumerations in a data dictionary.

Rename Enumerated Type Definition

  1. In the data dictionary, create a copy of the enumerated type, and rename the copy instead.

  2. Find enumeration objects used by your model that are derived from the type with the old name.

  3. Replace these objects with those derived from the renamed type.

  4. 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 dictionary enumDD.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

  1. Find references in your model to an enumeration member you want to delete.

  2. Replace these references with an alternate member.

  3. 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 of Simulink.Parameter objects.

  1. Find references in your model to an enumeration member whose value you want to change.

  2. Make a note of these references.

  3. Change the value of the enumeration member.

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

  1. Manually define the enumerated data type in MATLAB. See Use Enumerated Data in Simulink Models.

  2. 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 using Simulink.findIntEnumType and Simulink.clearIntEnumType.

  3. Delete the enumerated type from the dictionary.

See Also

Related Topics