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.

example

Examples

collapse all

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

collapse all

a — Content to be deserialized

object | structure

Content to be deserialized from the MAT file, which can be:

Output Arguments

collapse all

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

Version History

Introduced before R2006a