How do I listen for when new axes are added to a figure?

9 ビュー (過去 30 日間)
Matthew
Matthew 2019 年 6 月 6 日
回答済み: Daniel Dolan 2020 年 10 月 28 日
I would like to know when new axes are added to a figure. The axes will always have the figure as a parent. I have tried using the ChildAdded event listener, but this does not trigger when, for example, the subplot function creates a new set of axes.
  2 件のコメント
Steven Lord
Steven Lord 2019 年 6 月 6 日
How would you use this information if it was available? What's your use case? Knowing what your ultimate goal is may allow us to offer a better solution than trying to listen for new axes being added.
Matthew
Matthew 2019 年 6 月 6 日
We do a lot of copy and pasting of line objects between plots and usually, the line being pasted has the same style (color, lineStyle, etc.) as a line already on the axis. I would like to automatically changes that style to a unique one rather than forcing the user to manually do it all the tim.
Putting ChildAdded listeners on each axis was the only way that I could think of to trigger a callback when a line object is added to an axis, but that requires knowing all the axes in a figure. Therefore, I was asking how to listen for when an axis is added to a figure.
Any other ideas on how to do this would be appreciated.

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

採用された回答

Jan
Jan 2019 年 6 月 6 日
編集済み: Jan 2019 年 6 月 6 日
What about:
figure('DefaultAxesCreateFcn', 'disp(''hello'')');
subplot(1,2,1)
subplot(1,2,2)
To my surprise this shows 4 hello's in Matlab's LiveEditor. Maybe subplot creates more axes than I expect. Please check this by your own in a Matlab standalone environment.
By the way, such automatic code calls might look cool, but they increase the complexity of the code. Is there really a need to do this automatically? Using a modifed axes command, which performs the wanted job might be easier. Automagical code calls can confuse users massively and it impedes the debugging.
  1 件のコメント
Jan Kappen
Jan Kappen 2020 年 5 月 13 日
Jan, where does this function come from? It's nowhere documented, not in the figure properties, not in PropertyList of ?matlab.ui.Figure. Is it somewhere in the SuperClassList of the figure?
Background: I'm trying to achieve something similar as the OP. I want to trigger callbacks, when some children have changed. This can be useful for a figure override.
Since figure is sealed, no subclassing but only composition is possible. But I'd like to automatically apply some visual changes to Font Sizes, Axes ticks, Interpreter and so on on all axes/graphical elements being a child of a figure. I do not want to rewrite a complete new class with i.e. customcharts, I'd just like to instantiate a special "figureDin461" and apply very normal plot/subplot/limits etc. commands. Din461 is a standard on how plots shall look like in technical papers and publications, see https://de.wikipedia.org/wiki/DIN_461.

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

その他の回答 (1 件)

Daniel Dolan
Daniel Dolan 2020 年 10 月 28 日
Matthew,
I've been looking for a similar feature that keeps a particular axes on top of everything else. The ChildAdded listener triggers on menu, panel, and uicontrol additions, but not axes creation.
The best I have come up with is to monitor the figure's CurrentAxes property.
L=listener(gcf,'CurrentAxes','PostSet',@(~,~) disp('current axes changed'))
This appears to work for axes and subplot calls.
Dan

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by