Code Patterns for saveobj and loadobj - MATLAB & Simulink (original) (raw)

The recommended process for customizing serialization and deserialization of objects is to use the matlab.mixin.CustomElementSerialization mixin (since R2024b) and its associated classes to control how objects are serialized and deserialized. However, using the mixin does not work if:

Using saveobj and loadobj

Depending on the requirements of your class, there are various ways you can usesaveobj and loadobj methods. This pattern is a flexible way to solve problems that you cannot address by simpler means.

The basic process is:

This approach is not useful in cases where you cannot save property values in astruct field, such as a file identifier.

If you implement a saveobj method without implementing aloadobj method, MATLAB loads a default object of the class using the current class definition. Add a loadobj method to the class to create an object using the data saved with the saveobj method.

Use saveobj to Store Class Data as a Structure

Define saveobj as an ordinary method that accepts an object of the class and returns a struct.

methods function s = saveobj(obj) s.Prop1 = obj.Prop1; s.Prop2 = obj.Prop2 s.Data = obj.GraphHandle.YData; end end end

Use loadobj to Load Class Data from a Structure

Define loadobj as a static method. Create an object by calling the class constructor. Then assign values to properties from thestruct passed to loadobj. Use the data to regenerate properties that were not saved.

methods(Static) function obj = loadobj(s) if isstruct(s) newObj = ClassConstructor; newObj.Prop1 = s.Prop1; newObj.Prop2 = s.Prop2 newObj.GraphHandle = plot(s.Data); obj = newObj; else obj = s; end end end

If the load function encounters an error,load passes loadobj astruct instead of an object. Yourloadobj method must always be able to handle astruct as the input argument. The input toloadobj is always a scalar.

Using loadobj Without a Corresponding saveobj

loadobj can handle a struct input even if you are not using a saveobj method. For example, theGraphExpression class creates a graph of a MATLAB expression over a specified range of data.GraphExpression uses its loadobj method to regenerate the graph, which is not saved with the object.

classdef GraphExpression properties FuncHandle Range end methods function obj = GraphExpression(fh,rg) obj.FuncHandle = fh; obj.Range = rg; makeGraph(obj) end function makeGraph(obj) rg = obj.Range; x = min(rg):max(rg); data = obj.FuncHandle(x); plot(data) end end methods (Static) function obj = loadobj(s) if isstruct(s) fh = s.FuncHandle; rg = s.Range; obj = GraphExpression(fh,rg); else makeGraph(s); obj = s; end end end end

Create a GraphExpression instance with an anonymous function and a range of data as inputs.

h = GraphExpression(@(x)x.^4,[1:25])

h =

GraphExpression with properties:

FuncHandle: @(x)x.^4
     Range: [1x25 double]

Save the GraphExpression object and close the graph.

When you load the object, MATLAB recreates the graph.

saveobj and loadobj with Subclass Objects

If the most specific class of an object does not define a loadobj or saveobj method, the class can inherit loadobj or saveobj methods from a superclass.

If any class in the hierarchy defines saveobj orloadobj methods:

Reconstruct the Subclass Object from a Saved struct

Suppose that you want to save a subclass object by first converting its property data to a struct in the subclass saveobj method. Then you reconstruct the object when loaded using itsloadobj method. This action requires that:

The following superclass (MySuper) and subclass (MySub) definitions show how to code these methods.

classdef MySuper properties X Y end methods function S = saveobj(obj) S.PointX = obj.X; S.PointY = obj.Y; end function obj = reload(obj,S) obj.X = S.PointX; obj.Y = S.PointY; end end methods (Static) function obj = loadobj(S) if isstruct(S) obj = MySuper; obj = reload(obj,S); end end end end

Call the superclass saveobj and loadobj methods from the subclass saveobj and loadobj methods. (The loadobj method does this indirectly by calling thereload method, which in turn calls the superclassloadobj.)

classdef MySub < MySuper properties Z end methods function S = saveobj(obj) S = saveobj@MySuper(obj); S.PointZ = obj.Z; end function obj = reload(obj,S) obj = reload@MySuper(obj,S); obj.Z = S.PointZ; end end methods (Static) function obj = loadobj(S) if isstruct(S) obj = MySub; obj = reload(obj,S); end end end end

Additional Considerations for loadobj and saveobj

When you decide to modify the default save and load process withloadobj and saveobj, keep these points in mind:

See Also

Topics