How to automatically get the axis of a figure in Live Editor?

4 views (last 30 days)
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?
Thank you
  2 Comments
Grelier Matthieu
Grelier Matthieu on 2 May 2022
Thanks but this does not work inside the Live Editor it seems. I just display an image with imagesc and then I want to zoom on one of the feature and basically extract the associated axis limits. It's easily achieved outside the Live Editor but it seems the figures react differently in live scripts.

Sign in to comment.

Accepted Answer

Cris LaPierre
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;
plot(ax,x,y)
Option 2: plot your data as you normally would. Once created, capture the current axes using the gca command.
plot(x,y)
ax = gca;
This works for most plot types, but some plots may behave differently. If neither approach works for you, please share your code.
  3 Comments
Grelier Matthieu
Grelier Matthieu on 3 May 2022
I was calling the get function in the live editor. I see. I think your first idea is the best compromise so I'll go for it. Thank you.

Sign in to comment.

More Answers (1)

Steven Lord
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 =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties
f.Color = 'r';
The ancestor function lets you ask for other ancestors as well:
ax = ancestor(h, 'axes')
ax =
Axes with properties: XLim: [1 10] YLim: [1 10] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
isequal(f, ancestor(ax, 'figure')) % true
ans = logical
1
  1 Comment
Grelier Matthieu
Grelier Matthieu on 3 May 2022
Thanks for the answer. Same problem as before though, it works outside the Live Editor but not in it. It seems it cannot be done in lives scripts...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by