loadobj
Customize load process for objects
Syntax
Description
Note
The matlab.mixin.CustomElementSerialization
class is recommended over
loadobj
and saveobj
because the mixin
provides greater control over how objects are serialized and deserialized, including
the ability to add, delete, and rename properties. (since R2024b)
deserializes an object represented by b
= loadobj(a
)a
. Define a
loadobj
method when objects of a class require special processing
when loaded from MAT files. If you define a saveobj
method, then define a loadobj
method to restore
the object to the desired state. If the class of the object defines a
loadobj
method, load
automatically calls it.
Examples
Input Arguments
Output Arguments
Tips
Define
loadobj
as a static method.Implement your
loadobj
method to work with scalar objects or structures. When you deserialize an object array,load
callsloadobj
on each element of the saved array.When a new version of a class removes, renames, or changes the validation for a property,
load
can generate an error when attempting to set the altered or deleted property. If the class defines aloadobj
method, MATLAB returns the saved values to theloadobj
method in astruct
. If the saved object derives from multiple superclasses that have private properties with same name, thestruct
contains only the property value of the most direct superclass.When loading a subclass object,
load
calls only the subclassloadobj
method. If a superclass defines aloadobj
method, the subclass inherits this method. However, the inherited method might not perform the necessary operations to load the subclass object. Consider overriding superclassloadobj
methods if needed for your application. You can also call a superclassloadobj
from a subclassloadobj
.
Version History
Introduced before R2006a