Observe graphics object added to the axes

1 回表示 (過去 30 日間)
Zoltán Csáti
Zoltán Csáti 2017 年 10 月 30 日
編集済み: uno 2021 年 9 月 13 日
I have an axes object which contains several child objects (lines, patches, etc.). I have a function which sets the callback function for these objects. What I want is that whenever the user adds another object to the axes, an event is activated and sets the callback for the newly added object. My idea was to look when the number of the children of axes grow as it indicates a new object. Unfortunately, axes.Children is not a SetObservable property, so listeners cannot be attached to the axes object. Any ideas how to solve this problem?

回答 (2 件)

Roman Müller-Hainbach
Roman Müller-Hainbach 2017 年 11 月 2 日
Maybe the 'LegendableObjectsUpdated' event could be used for your purposes. I retrieved this from
events('matlab.graphics.axis.Axes')
  2 件のコメント
Jan Kappen
Jan Kappen 2020 年 5 月 13 日
This does not work anymore, since most of the events are hidden.
Workaround:
m = ?matlab.graphics.axis.Axes;
e = m.EventList
Try it out, there are tons of events.
Zoltán Csáti
Zoltán Csáti 2020 年 5 月 13 日
Thank you. I will try it out once I have access to MATLAB. On this computer, I don't have a license, and on the other computer, I have R2015b. The poor backward compatibility in MATLAB is a pity...

サインインしてコメントする。


uno
uno 2021 年 9 月 13 日
編集済み: uno 2021 年 9 月 13 日
You can use a wrapper to change an observable property every time you plot
function main()
ax = axes;
propListener = addlistener(ax, 'UserData', 'PostSet', @(src,evnt)mycallback(evnt.AffectedObject)); % Attach the callback to the UserData property
myplot(ax,1,1) % Wrapper of plot(), 1st argument must be the target
end
function mycallback(ax)
% Do something with ax.Children
end
function myplot(varargin) % Wrapper function
plot(varargin{:});
varargin{1}.UserData = 1; % Overwrite UserData to trigger the listener event
end
Or just ditch the callbacks entirely and save yourself a lot of troubles..
function myplot(varargin)
h = plot(varargin{:});
% Do something with the new objects
end

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by