Throw error from PreSet event callback
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
sanidhyak
el 6 de Mzo. de 2025
I understand that you are facing an issue where MATLAB converts the error inside your “PreSet” event callback into a warning instead of stopping execution. This occurs because MATLAB handles errors inside event callbacks differently, often preventing them from stopping property changes.
To resolve this, you can explicitly revert the property change inside the callback before throwing the error. Kindly refer to the following corrected implementation:
classdef MyClass < handle
properties(SetObservable)
Value
end
properties
locked = false;
end
methods
function obj = MyClass()
add_listeners(obj);
end
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);
end
end
end
function handle_preset_event(obj, prop, event)
if obj.locked
event.Source.(prop.Name) = event.AffectedObject.(prop.Name); % Revert change
error('myclass:property_locked', ...
['Property ' prop.Name ' cannot be changed; class instance is locked.']);
end
end
end
end
Here, the “PreSet” listener is added for all “SetObservable” properties in the constructor. Also, the “handle_preset_event” function checks if locked is true. If locked, it explicitly reverts the property change before throwing an error, ensuring MATLAB does not override it with a warning.
With these changes, MATLAB will prevent modifications when the object is locked, and an actual error will be thrown instead of a warning.
For further reference on MATLAB “event listeners”, kindly refer to following documentation:
Cheers & Happy Coding!
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!