Throw error from PreSet event callback

2 vues (au cours des 30 derniers jours)
Christian Vetter
Christian Vetter le 23 Avr 2020
Hi everyone,
I have a little problem with a PreSet event callback which I would like to implement. My goal is to check whether my property "locked" is true. If this is the case, Matlab should throw an error. Unfortunately, it seems Matlab converts my error to a warning, which means that the code continues to execute. Is there any way to make an error inside an event callback an actual error?
For clarification ... You may wonder why I do not implement that via a set function. The reason is that this implementation is supposed to affect quite a few properties over several levels of inheritance. If I would use set functions, I would have to define them for all properties individually and they would all do absolutely the same. Right now, my top level class has a function which looks like that:
function add_listeners(obj)
mc = metaclass(obj);
mp = mc.PropertyList;
for run = 1:length(mp)
if mp(run).SetObservable
addlistener(obj,mp(run).Name,'PreSet',@obj.handle_preset_event);
addlistener(obj,mp(run).Name,'PostSet',@obj.handle_postset_event);
end
end
end
It is called inside the constructor method and adds listeners for all properties that are "SetObservable". My "handle_preset_event" function currently looks like that:
function handle_preset_event(obj, prop, ~)
if obj.locked
error('myclass:property_locked', ...
['Property ' prop.Name ' cannot be changed; class instance is locked.'])
end
end
But as mentioned above, this does not work as indended because Matlab converts the error to a warning.
Any ideas on that?
Thanks a lot in advance.
PS: Other solutions are welcome as well, of course. I just don't want to copy the same part of my code 20 times.

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by