Automatic Updates for Modified Classes - MATLAB & Simulink (original) (raw)

When MATLAB Loads Class Definitions

MATLAB® loads a class definition:

MATLAB allows only one definition for a class to exist at any time. Therefore, MATLAB attempts to update all existing objects of a class automatically to conform to the new class definition. You do not need to call clear classes to remove existing objects when you change their defining class.

Note

Using an editor other than the MATLAB editor or using MATLAB Online™ can result in delays to automatic updating.

Consequences of Automatic Update

MATLAB follows a set of basic rules when updating existing objects. An automatic update can result in:

Here is an example of what happens when you create an instance of a concrete class edit the class definition to make the class abstract.

a = MyClass; % Edit MyClass to make it Abstract

a

Error using MyClass/display Cannot update object because the class 'MyClass' is now abstract.

Note

MATLAB does not update metaclass instances when you change the definition of a class. You must get new metaclass data after updating a class definition.

What Happens When Class Definitions Change

MATLAB updates existing objects when a class definition changes, including the following situations:

Ensure Defining Folder Remains in Scope

Changes to the MATLAB path that result in removing the class definition file from the path, even temporarily, can produce side effects. If a function changes from the current folder, which contains the class definition, and that folder is not on the path, then the function cannot call methods of the class that is now out of scope. To avoid potential problems, add the class defining folder to the path before changing to another folder.

For example, suppose the class of the input obj is defined in the current folder, which is not on the path. Before changing the current folder to another folder, add the current folder to the path using the addpath function.

function runFromTempFolder(obj) % Add current folder to path addpath(pwd) definingFolder = cd('myTempFolder'); obj.myMethod; cd(definingFolder) end

Actions That Do Not Trigger Updates

These actions do not update existing objects:

Objects do not update until referenced in a way that exposes the change, such as invoking the object display or assigning to a property.

Multiple Updates to Class Definitions

Updates do not occur incrementally. Updates conform to the latest version of the class.

Object Validity with Deleted Class File

Deleting a class definition file does not make instances of that class invalid. However, you cannot call methods on existing objects of that class.

When Updates Are Not Possible

Some class updates result in an invalid class definition. In these cases, objects do not update until the error is resolved:

Some class updates cause situations in which MATLAB cannot update existing objects to conform to a modified class definition. These cases result in errors until you delete the objects:

Potential Consequences of Class Updates

Interactions with the Debugger

Since R2021a.

MATLAB disables the debugger during class updates. Before R2021a, a breakpoint could potentially interrupt the class update process and allow for the introduction of errors when the update resumes. For example, the breakpoint allows a class author to introduce invalid syntax into the class definition or remove the class from the path entirely, potentially causing MATLAB to crash.

Example of a Disabled Breakpoint

This class defines a property validation function:

classdef ClassWithBreakpoint properties (Constant) Prop1 (1,1) {myPropertyValidator} = 32 end end

function myPropertyValidator(~) end % Add breakpoint here

Create an instance of this class. Then add a breakpoint where indicated, and update the definition of Prop1 to a different initial value:

Prop1 (1,1) {myPropertyValidator} = 10

In version R2020b and earlier, MATLAB hits the breakpoint, and the class update is interrupted. In R2021a, the debugger is disabled, and the breakpoint does not interrupt the update.

Updates to Class Attributes

Changing class attributes can change existing object behavior or make the objects invalid. MATLAB returns an error when you access the invalid objects.

Change Effect
Make Abstract = true Accessing existing objects returns an error.
Change AllowedSubclasses Newly created objects can inherit from different superclasses than existing objects.
Change ConstructOnLoad Loading classes obeys the current value of ConstructOnLoad.
Change HandleCompatible Newly created objects can have different class hierarchy than existing objects.
Change Hidden Appearance of class in list of superclasses and access by help function can change
Change InferiorClasses Method dispatching for existing objects can change.
Make Sealed = true Existing subclass objects return errors when accessed.

Updates to Property Definitions

When you change the definition of class properties, MATLAB applies the changes to existing objects of the class.

Change Effect
Add property Adds the new property to existing objects of the class. Sets the property values to the default value (which is [] if the class definition does not specify a default).
Remove property Removes the property from existing objects of the class. Attempts to access the removed property fail.
Change property default value Does not apply the new default value to existing objects of the class.
Move property between subclass and superclass Does not apply different default value when property definition moves between superclass and subclass.
Change property attribute value Applies changes to existing objects of the class.Some cases require transitional steps:Abstract — Existing objects of a class that becomes abstract cannot be updated. Delete these objects.Access — Changes to the public, protected, or private property access settings affect access to existing objects.Changes to the access lists do not change existing objects. However, if you add classes to the access list, instances of those classes have access to this property. If you remove classes from the access list, objects of those classes no longer have access to this property.Dependent — If changed to true, existing objects no longer store property values. If you want to query the property value, add a property get method for the property.Transient — If changed to true, objects already saved, reload this property value. If changed to false, objects already saved reload this property using the default value.

Updates to Method Definitions

When you change the definition of class methods, MATLAB changes the affected class member in existing objects as follows.

Change Effect
Add method You can call the new method on existing objects of the class.
Modify method Modifications are available to existing objects.
Remove method You can on longer call deleted method on existing objects.
Change method attribute value Apply changes to existing objects of the class.Some cases require transitional steps:Abstract — Existing objects of a class that becomes abstract cannot be updated. Delete these objects.Access — Changes to method public, protected, or private access settings affect access to existing objects.Changes to the access lists do not change existing instances. However, if you add classes to the access list, instances of those classes have access to this method. If you remove classes from the access list, objects of those classes no longer have access to this method.Sealed — If changed to true and existing subclasses already have defined the method, MATLAB returns an error because the new class definition cannot be applied to existing subclasses.

Updates to Event Definitions

Change Effect
Add event Existing objects of the class support the new event.
Change event name New event name is visible to existing objects of the class. MATLAB:Does not update existing metaclass objectsDoes update newly acquired metaclass objectsDoes not update listeners to use new event name
Remove event Existing objects no longer support deleted event.
Change event attribute value Apply changes to existing objects of the class.Some cases require transitional steps:ListenAccess — Changes to event public, protected, or private listen access settings affect access to existing objects.Changes to the access list do not change existing objects. However, if you add classes to the access list, objects of those classes can create listeners for this event. If you remove classes from the access list, objects of those classes are not allowed to create listeners for this event.NotifyAccess — Changes to event public, protected, or private notify access settings affect access to existing objects.Changes to the access list do not change existing objects. However, if you add classes to the access list, instances of those classes can trigger this event. If you remove classes, objects of those classes are not able to trigger this event.

See Also

Topics