loadobj - Customize load process for objects - MATLAB (original) (raw)
Customize load process for objects
Syntax
Description
Note
The matlab.mixin.CustomElementSerialization class is recommended overloadobj
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)
[b](#mw%5F6f39094a-b09e-4d5c-b948-26f308176d7b) = loadobj([a](#mw%5F99d96b3c-1005-4a55-9490-68b458061912))
deserializes an object represented by a
. Define aloadobj
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 aloadobj
method, load automatically calls it.
Examples
Restore Listener Using loadobj
Listeners cannot be serialized with objects. If your class defines a listener, you can use loadobj
to restore it when an instance is deserialized.
Define the BankAccount
class. The class includesAccountBalance
and AccountStatus
properties. The transient property AccountManagerListener
holds a listener object that changes AccountStatus
based on the value ofAccountBalance
.
classdef BankAccount < handle properties (SetObservable,AbortSet) AccountBalance end properties (Transient) AccountManagerListener end properties AccountStatus end methods function obj = BankAccount(initialBalance) obj.AccountBalance = initialBalance; obj.AccountStatus = "New Account"; obj.AccountManagerListener = addlistener(obj,... "AccountBalance","PostSet",@(src,evt)assignStatus(obj)); end function assignStatus(obj,~) if obj.AccountBalance < 0 obj.AccountStatus = "Overdrawn"; else obj.AccountStatus = "Open"; end end end end
Create an instance of BankAccount
.
x =
BankAccount with properties:
AccountBalance: 100
AccountManagerListener: [1×1 event.proplistener]
AccountStatus: "New Account"
Set the AccountBalance
property to a negative amount and check the change to AccountStatus
.
x =
BankAccount with properties:
AccountBalance: -100
AccountManagerListener: [1×1 event.proplistener]
AccountStatus: "Overdrawn"
If you call save
to serialize x
,AccountManagerListener
is not serialized. To restore the event listener when you call load
, define a loadobj
method for BankAccount
that recreates the listener and assigns it toAccountManagerListener
.
methods (Static) function obj = loadobj(obj) if isstruct(obj) initialBalance = obj.AccountBalance; obj = BankAccount(initialBalance); else obj.AccountManagerListener = addlistener(obj,... "AccountBalance","PostSet",@(src,evt)assignStatus(obj)); end end end
Save x
and then clear it. Load x
to confirmloadobj
recreated the event listener.
save("C:_yourpath_\AccountTest.mat","x") clear x load("C:_yourpath_\AccountTest.mat","x") x
x =
BankAccount with properties:
AccountBalance: -100
AccountManagerListener: [1×1 event.proplistener]
AccountStatus: "Overdrawn"
Input Arguments
a
— Content to be deserialized
object | structure
Content to be deserialized from the MAT file, which can be:
- An object
- A structure created by
load
ifload
cannot resolve the object - A structure created by the corresponding
saveobj
method
Output Arguments
b
— Object passed to load
function
object
Object passed to the load
function by MATLAB®.The value returned by a class loadobj
method is typically an object of the class being loaded. However, the loadobj
method can return an object of a different class or an updated object that matches a new class definition.
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 a loadobj 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