MATLAB Object-Oriented Programming

(Joyce) #1

Initialize Objects When Loading


In this section...
“Calling Constructor When Loading Objects” on page 13-30
“Initializing Objects in the loadobj Method” on page 13-30

Calling Constructor When Loading Objects


MATLAB does not call the class constructor when loading an object from a MAT-file.
However, if you set the ConstructOnLoad class attribute to true, load does call the
constructor with no arguments.

Enable ConstructOnLoad when you do not want to implement a loadobj method, but
must perform some actions at construction time. For example, enable ConstructOnLoad
when you are registering listeners for another object. Ensure that MATLAB can call the
class constructor with no arguments without generating an error.

Attributes set on superclasses are not inherited by subclasses. Therefore, MATLAB does
use the value of the superclass ConstructOnLoad attribute when loading objects. If you
want MATLAB to call the class constructor, set the ConstructOnLoad attribute in your
specific subclass.

If the constructor requires input arguments, use a loadobj method.

Initializing Objects in the loadobj Method


Use a loadobj method when the class constructor requires input arguments to perform
object initialization.

The LabResults class shares the constructor object initialization steps with the
loadobj method by performing these steps in the assignStatus method.

Objects of the LabResults class:


  • Hold values for the results of tests.

  • Assign a status for each value based on a set of criteria.


classdef LabResult
properties

13 Saving and Loading Objects

Free download pdf