Add a event callback across all instances of class??
Mostra commenti meno recenti
The idea is as follows:
If a property of an instance of class is changed, then all the instances of this class should take this property and do some update. The problem is I don't know how many instances of class exist, so I thought using event and listener could be a solution. Here is my code:
classdef myclass < handle
%EVENTTAKER Summary of this class goes here
% Detailed explanation goes here
properties
hf
sharedProp
end
events
sharedPropChanged
end
methods
function obj = myclass()
obj.sharedProp = rand(100, 2); % define prop as random data
obj.hf = figure;
plot(obj.sharedProp);
addlistener(obj,'sharedPropChanged',@obj.listenerCallback);
end
function setSharedProp(obj, newProp)
obj.sharedProp = newProp;
% broadcast event to all objects of class
notify(obj, 'sharedPropChanged');
end
function listenerCallback(obj, srcObj, ~)
obj.sharedProp = srcObj.sharedProp;
obj.update();
end
function update(obj)
plot(obj.sharedProp);
end
end
end
To test:
o1 = myclass; o2 = myclass;
o1.setSharedProp(rand(10, 2))
The result is that only the first instance get update, the second not.
Is this a plausible solution, or there is any way to implement this?
Thnks
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Properties in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!