Main Content

loadobj

Customize load process for objects

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)

b = loadobj(a) deserializes an object represented by 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.

example

Examples

collapse all

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 includes AccountBalance and AccountStatus properties. The transient property AccountManagerListener holds a listener object that changes AccountStatus based on the value of AccountBalance.

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(100)
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.AccountBalance = -100
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 to AccountManagerListener.

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 confirm loadobj 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

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

  • An object

  • A structure created by load if load cannot resolve the object

  • A structure created by the corresponding saveobj method

Output Arguments

collapse all

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 calls loadobj 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 the loadobj method in a struct. If the saved object derives from multiple superclasses that have private properties with same name, the struct contains only the property value of the most direct superclass.

  • When loading a subclass object, load calls only the subclass loadobj method. If a superclass defines a loadobj method, the subclass inherits this method. However, the inherited method might not perform the necessary operations to load the subclass object. Consider overriding superclass loadobj methods if needed for your application. You can also call a superclass loadobj from a subclass loadobj.

Version History

Introduced before R2006a