How to automatically get the axis of a figure in Live Editor?
5 views (last 30 days)
Show older comments
Grelier Matthieu
on 2 May 2022
Commented: Grelier Matthieu
on 3 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?
Thank you
2 Comments
Riccardo Scorretti
on 2 May 2022
Normally, if you create new figures by:
hndl = figure();
you should get the handle to the figure. Can you provide a simple example of what you need to do?
Accepted Answer
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
Cris LaPierre
on 2 May 2022
I see. Where are you calling the get function? In the live editor or in the command window?
I think what is happening here is that the image you see in the livescript is a snapshot of the figure at a specific time, and not the actual figure. So while you can zoom in and otherwise interact with the snapshot, it is not the axes captured in ax.
I'm afraid I don't know a way to do exactly what you want using the image embedded in a live script. Ideas that immediately come to mind are to either
1. use the Update Code option to add code to your script to update the actual figure axes to those you established via manually zooming. This appears automatically

2. pop the figure out of the live script so that it behaves as you are expecting. You can do that programmatically with the following code. Now when you interact with the figure window that opens, you are modifying the axes captured in ax.
figure('Visible','on')
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
See Also
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

