Modifying and Reloading Classes
Ensuring MATLAB Uses Your Changes
There is only one class definition for a given class in MATLAB at any given time. When you create an instance of a class, MATLAB loads the class definition. So as long as instances of that class exist, MATLAB does not reload the class definition.
Clear Class Instances
When you modify a class definition, the current MATLAB session continues to use the original class definition until you clear all objects of that class. For example, if obj1 and obj2 are instances of a class for which you have modified the class definition, clear those objects so MATLAB can use your changes. Use the clear command to remove only those instances:
clear obj1 obj2
Modifying a class definition includes doing any of the following:
- Changing class member attributes
- Adding, deleting, or changing the names of properties, methods, or events
- Changing class inheritance
- Changing the definition of a superclass (requires you to clear subclass objects)
Clear Classes
If there are no class instances, MATLAB applies changes in the code immediately. If there are instances, you must clear those objects before MATLAB applies your changes.
When you issue the clear classes command, MATLAB clears:
- The current workspace of all variables
- All functions, which can have persistent variables holding class instances (unless the function is locked)
- All classes that are not instantiated
However, it is possible that your MATLAB session is holding instances of the class that the clear classes command does not clear. For example, suppose you change the definition of MyClass after saving an instance of this class in a Handle Graphics® object’s UserData property:
obj = MyClass; % User-defined class that you are editing
h = uicontrol('Style','pushbutton');
set(h,'UserData',obj)
clear classes
Warning: Objects of 'MyClass' class exist. Cannot clear this class or any of its
super-classes.
MATLAB issues a warning stating that it cannot apply your changes because it cannot clear the class. Clear the instance of MyClass before calling clear classes. For example, you can use the close all command to remove the object or reset the UserData property to another value:
% First, get the handle of the uicontrol, which was cleared
h = findobj('Type','uicontrol','Style','pushbutton');
set(h,'UserData',[])
[help]Now you can issue the clear classes command.[/help]
Places That Can Hold Instances
You can remove class instances from your workspace using the clear obj… command. However, as the preceding example shows, objects can be held in various ways. Clear all instances before MATLAB applies your new class definition.
Here are some suggestions for finding and clearing class instances:
Persistent Variables: Persistent variables can hold objects. Clear persistent variables using clear functions. If the function containing the persistent variable is locked, then unlock the function (using munlock) before clearing it.
Locked Functions: Functions can contain objects in their workspace. If the function is locked (with mlock), unlock it (using munlock) so that MATLAB can clear the instance. Use clear functions once you have unlocked the function.
Default Property Values: When you specify a default value in a properties definition block, MATLAB evaluates the expression that defines the default value once when loading the class. Clear this value using the clear classes command.
Constant Properties: When you define a constant property (property Constant attribute set to true) whose value is an object, MATLAB creates the instance when loading the class. Clear this instance using the clear classes command.
Handle Graphics Objects: Handle Graphics objects can contain class instances in UserData properties, in Application Data, or created in callback functions. Issuing the close all command removes the Handle Graphics objects, unless these objects enable hidden handles. See the close command for more information. You can remove Application Data using the rmappdata function.
Simulink Models: Models can contain class instances. Use close_system to close the model so that MATLAB can apply your changes.