addlistener error - updating from handle.listener

6 views (last 30 days)
Hi,
I am trying to edit a code which I didnt write that uses handle.listener in it. I am struggling to update it to meet the requirements of addlistener.
The original code is:
listener = handle.listener(parent_handle, ...
parent_handle.findprop(property), ...
'PropertyPostSet', update_fcn);
I have modified it to:
listener = addlistener(parent_handle, ...
parent_handle.findprop(property), ...
'PostSet', update_fcn);
However, I am still receiving the error message "While adding a PostSet listener, property 'Position' in class 'matlab.ui.Figure' is not defined to be SetObservable."
How can i resolve this? Thanks!!

Answers (1)

Divyajyoti Nayak
Divyajyoti Nayak on 27 Dec 2024
Hi Kaden,
According to the documentation of the ‘addlistener’ function, only properties whose class can set the ‘GetObservable’ and ‘SetObservable’ property attributes can be listened to. The ‘Position’ property of the figure object is not such a property and hence you are getting the error. Here’s some documentation on the property requirements of the ‘addlistener’ function:
I did find a workaround in StackOverflow that uses ‘JavaFrame’ to add a callback when the figure is moved. Here’s a sample code:
a = figure;
pause(0.2) % Wait for the figure construction complete.
jFig = get(a, 'JavaFrame'); % get JavaFrame. You might see some warnings.
jWindow = jFig.fHG2Client.getWindow;
jbh = handle(jWindow,'CallbackProperties'); % Prevent memory leak
set(jbh,'ComponentMovedCallback',@callback);
function callback(src,event)
disp('Update');
end
Here’s the link to the StackOverflow answer (the second answer by Y.Chang)

Categories

Find more on Handle Classes 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!