addlistener error - updating from handle.listener
7 次查看(过去 30 天)
显示 更早的评论
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!!
0 个评论
回答(1 个)
Divyajyoti Nayak
2024-12-27
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Handle Classes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!