How to automatically get the axis of a figure in Live Editor?
4 views (last 30 days)
Grelier Matthieu on 2 May 2022
I'm working with images on a live script and it would be much faster for me if it was possible to get the figure axis. However I don't even manage to get the figure handles so I don't know if a solution exists?
Cris LaPierre on 2 May 2022
Edited: Cris LaPierre on 2 May 2022
You can get the axes object in a figure the same way you would outside of a live script.
Option 1: capture the axes object by creating it with code. Be sure to specify the target axes when plotting, or the plot command will create a new figure and axes.
ax = axes;
Option 2: plot your data as you normally would. Once created, capture the current axes using the gca command.
ax = gca;
This works for most plot types, but some plots may behave differently. If neither approach works for you, please share your code.
More Answers (1)
Steven Lord on 2 May 2022
If you call the function that displays the image with an output argument, you can determine which figure contains that graphics object using the ancestor function.
h = plot(1:10);
f = ancestor(h, 'figure')
f.Color = 'r';
The ancestor function lets you ask for other ancestors as well:
ax = ancestor(h, 'axes')
isequal(f, ancestor(ax, 'figure')) % true