Why do the listeners I define for my class break when I use the PACK command in MATLAB 7.9 (R2009b)?
2 views (last 30 days)
Show older comments
I have created a simple handle class that notifies on specific events. I have another handle class that listens to these events.
When I create listeners, they all work until I issue the PACK command. The documentation does not mention anything about listener classes breaking when the PACK command is used.
For example, if I define the classes "ToggleButton" and "RespondToToggle" as follows:
classdef ToggleButton < handle
properties
State = false
end
events
ToggledState
end
methods
function OnStateChange(obj,newState)
% Call this method to check for state change
if newState ~= obj.State
obj.State = newState;
notify(obj,'ToggledState'); % Broadcast notice of event
end
end
end
end
classdef RespondToToggle < handle
methods
function obj = RespondToToggle(toggle_button_obj)
addlistener(toggle_button_obj,'ToggledState',@RespondToToggle.handleEvnt);
end
end
methods (Static)
function handleEvnt(src,evtdata)
if src.State
disp('ToggledState is true') % Respond to true ToggleState here
else
disp('ToggledState is false') % Respond to false ToggleState here
end
end
end
end
They work fine if I create the objects and try to generate events:
tb = ToggleButton;
rtt = RespondToToggle(tb);
tb.OnStateChange(true)
ToggledState is true
tb.OnStateChange(false)
ToggledState is false
However, if I save the objects and reload them, the events no longer trigger.
save myobjs tb rtt
clear all
load myobjs
tb.OnStateChange(true)
Accepted Answer
MathWorks Support Team
on 3 Dec 2009
The listeners stop working after the PACK command is issued because PACK saves the objects to file and reloads them. The save process breaks the connection between listeners, since listeners are attached to their objects only while the object is in scope. Once the object is saved, it goes out of scope, therefore the listener for the saved object is destroyed.
In order to keep the listener with the object when it is loaded back into the workspace, add the event-generating object as a property to the listening-object, and create a static LOADOBJ method for the listening class(es) that relinks the listener to the event generator.
In our example we would modify RESPONDTOTOGGLE as follows:
classdef RespondToToggle < handle
properties
toggle_button_obj
end
methods
function obj = RespondToToggle(toggle_button_obj)
addlistener(toggle_button_obj,'ToggledState',@RespondToToggle.handleEvnt);
obj.toggle_button_obj = toggle_button_obj;
end
end
methods (Static)
function handleEvnt(src,evtdata)
if src.State
disp('ToggledState is true') % Respond to true ToggleState here
else
disp('ToggledState is false') % Respond to false ToggleState here
end
end
function obj = loadobj(obj)
addlistener(obj.toggle_button_obj,'ToggledState',@RespondToToggle.handleEvnt);
end
end
end
0 Comments
More Answers (0)
See Also
See Also
Categories
Find more on Object Save and Load in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!