Saving and Loading Enumerations

Basic Knowledge

See the save and load functions and Understanding the Save and Load Process for general information on saving and loading objects.

See the enumeration function to list enumeration names.

Built-In and Value-Based Enumeration Classes

When you save enumerations that derive from built-in classes or that are value-based classes with properties, MATLAB® saves the names of the enumeration members and the definition of each member.

When loading these types of enumerations, MATLAB preserves names over underlying values. If the saved named value is different from the current class definition, MATLAB uses the value defined in the current class, and then issues a warning.

Simple and Handle-Based Enumeration Classes

When you save simple enumerations (those having no properties, superclasses, or values associated with the member names) or those enumerations derived from the handle class, MATLAB saves the names and any underlying values.

However, when loading these types of enumerations, MATLAB does not check the values associated with the names in the current class definition. This behavior results from the fact that simple enumerations have no underlying values and handle-based enumerations can legally have values that are different than those defined by the class.

Causes: Loading as Struct Instead of Object

The addition of a new named value or a new property made to a class subsequent to saving an enumeration does not trigger a warning during load.

If there are changes to the enumeration class definition that do not prevent MATLAB from loading the object (that is, all of the named values in the MAT-File are present in the modified class definition), then MATLAB issues a warning that the class has changed and loads the enumeration.

In the following cases, MATLAB issues a warning and loads as much of the saved data as possible as a struct:

  • MATLAB cannot find the class definition

  • The class is no longer an enumeration class

  • MATLAB cannot initialize the class

  • There is one or more enumeration member in the loaded enumeration that is not in the class definition

  • For value-based enumerations with properties, a property exists in the file, but is not present in the class definition

Struct Fields

The returned struct has the following fields:

  • ValueNames — A cell array of strings, one per unique value in the enumeration array.

  • Values — An array of the same dimension as ValueNames containing the corresponding values of the enumeration members named in ValueNames. Depending on the kind of enumeration class, Values can be one of the following:

    • If the enumeration class derives from a built-in class, the array is of the built-in class and the values in the array are the underlying values of each enumeration member.

    • Otherwise, a struct array representing the property name — property values pairs of each enumeration member. For simple and handle-based enumerations, the struct array has no fields.

  • ValueIndices — a uint32 array of the same size as the original enumeration. Each element is an index into the ValueNames and Values arrays. The content of ValueIndices represents the value of each object in the original enumeration array.

Was this topic helpful?