Main Content

Save and Load Enumerations

Basic Knowledge

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

To see a list of enumeration names defined by a class, use the enumeration function.

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 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 that have no properties, superclasses, or values associated with the member names or enumerations derived from the handle class, MATLAB saves the names and any underlying values.

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 values defined by the class.

Causes: Load as struct Instead of Object

If you add a new named value or a new property to a class after saving an enumeration, MATLAB does not warn during load.

If the changes to the enumeration class definition do not prevent MATLAB from loading the object (that is, all 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 these five cases, MATLAB issues a warning. In case 1, there are no defined results. In cases 2 through 5, MATLAB loads as much of the saved data as possible as a struct:

  1. MATLAB cannot find the class definition.

  2. The class is no longer an enumeration class.

  3. MATLAB cannot initialize the class.

  4. There are one or more enumeration members in the loaded enumeration that is not in the class definition.

  5. If the class is a value-based enumeration with properties and a property that exists in the file, is not present in the class definition.

struct Fields

In cases 2 through 5, the returned struct has these 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 these:

    • If the enumeration class derives from a built-in class, the array class is the same as the built-in class. 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.

Related Topics