Live scripts: Get axes handle of figure NOT stored in live script.

3 ビュー (過去 30 日間)
Nicholas Ayres
Nicholas Ayres 2020 年 7 月 9 日
編集済み: Adam Danz 2022 年 8 月 8 日
I have a function which has plotted a series of functions as non-livescript plots, but not passed the plot handles out.
I am trying to use gca within my code to grab the most recently selected plot.
However, since (I assume) the live script is based off some form of gui, the gca function only grabs the handle of (I assume) plots within the live script window itself.
How do I grab the handle of a plot that is not located in the livescript?
  1 件のコメント
Julian Dönges
Julian Dönges 2022 年 8 月 6 日
I am also looking for a solution to do this programmatically, evalin and findobj do not work.
You can type h = gca into the command window and get the handle manually, but that is not always feasible.

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

回答 (1 件)

Adam Danz
Adam Danz 2022 年 8 月 6 日
編集済み: Adam Danz 2022 年 8 月 8 日
This is why gca should usually be avoided.
Here are some solutions in order of robustness.
  1. Change the function so that it outputs the axis handle (or at least a handle to an object within the axes, see #2). Alternatively, you could create the axes ahead of time, store the handle, and then call your function if your function is set up to work with pre-existing axes.
  2. If you have the handle (h) to an object within the axes use ax=ancestor(h,'axes')
  3. If you have the figure handle (f), use ax=gca(f), assuming there is only 1 axes in the figure.
  4. This one is a worst-case-scenario but if you know the axes was just generated and gca is returning the wrong axes, you can record a list of axes prior to the line that creates the axes and then store another list of all axes after the axes is created. Then compare the two lists to find the new one:
% option 4
originalAxesList = findall(groot,'type','axes');
nexttile % example, call function that creates new axes
updatedAxesList = findall(groot,'type','axes');
isNewAxes = ~ismember(updatedAxesList, originalAxesList);
newAxes = updatedAxesList(isNewAxes)

カテゴリ

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by