Skip to content

addlistener

matlab_compat · scripts/matlab_compat/addlistener.m

ADDLISTENER Create an event or property listener (MATLAB compat).

lh = addlistener (obj, EVENTNAME, CALLBACK) lh = addlistener (obj, PROP, ’PreSet’|’PostSet’|’PreGet’|’PostGet’, CALLBACK)

For a classdef handle OBJ, EVENTNAME must be declared in an events block (or be a built-in handle event). PROP must be SetObservable / GetObservable as required by the property event. CALLBACK is a function handle (or cell array whose first element is one) called as CALLBACK (src, eventData, ...).

Returns an event.listener (or event.proplistener). Unlike constructing those classes directly, addlistener ties the listener to OBJ so it stays alive until delete (lh) or OBJ is destroyed.

A compiled builtin of the same name also exists (graphics / Java / classdef). This .m file matches MATLAB’s classdef signatures when the builtin is not selected; keep it in sync with event.listener / event.proplistener.

Decisions (MATLAB R2026a): - Unknown event → MATLAB:class:invalidEvent when events() lists names - When events() is empty (meta not yet populated), skip name check so the listener still registers (legacy Nexus builds).