91 views (last 30 days)
Show older comments
Amol Khalate on 6 Aug 2013
Edited: dpb on 24 Oct 2017
Dear all,
I've a saved Matlab figure, which has several subplots. I would like to know how to get handles for different subplots after opening this figure.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (1)
dpb on 6 Aug 2013
Open in MATLAB Online
A) If you know the number of subplots/orientation a priori, then
h=subplot(n,m,i);
will return the handle for the existing subplot at that location when gcf is the correct figure.
B) To find them not knowing the above, after hgload()
h=get(gcf,'children');
as the axes are the children of the figure; other stuff is descended from the axes object. Can always check on the above to ensure that
get(get(gcf,'children'),'type')
returns axes
7 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
Lucky Tran on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496171
Edited: Lucky Tran on 23 Oct 2017
A) Does not work because h=subplot() overwrites the existing subplot and returns handles to the newly created subplot.
B) Works but the explanation is incomplete. The axes handles need to be accessed using h(1), h(2), etc.
dpb on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496242
A) is NOT h=subplot(); but h=subplot(n,m,i); which WILL return the handle of the existing ith subplot without erasing it, and
B) is complete as to what the question raised; namely how to get the handles of the existing subplots.
Walter Roberson on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496273
h = subplot(n, m, i) will delete any existing axes that is overlayed by the implied position of the subplot, except that if there is an existing axes with that exact position then it will return the handle of that one axes instead of destroying it along with the other ones it overlaps.
Walter Roberson on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496275
When you h=get(gcf,'children') or h = findobj(gcf, 'type', 'axes') then the order of the various subplots in the children will match whatever order was in the figure when it was saved, which in turn depends upon the order in which the user created those axes. There is unfortunately no information saved with a subplot axes to indicate what combination of n, m, i were used to create it, so you need to deduce the relative subplot locations by examining the Position information.
dpb on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496288
NB: the given caveat on A) above...
"If you know the number of subplots/orientation a priori..."_ which is precisely the case in which the function returns the handle of the existing supblot at that place/orientation.
As for the latter; sure, if one doesn't save handles a priori and must go "handle-diving" to retrieve them later, more effort may (will be) required to use them than if had saved them in the first place.
Walter Roberson on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496291
If you had tweaked the position of the subplots at all then subplot() is probably going to destroy rather than find what you want :(
... Probably the easiest way to handle all this is to assign distinct tags to each subplot when it is created, and then to later retrieve by tag.
dpb on 23 Oct 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/84163-getting-handle-for-subplot-of-saved-figure#comment_496292
Edited: dpb on 24 Oct 2017
True, but that ship had sailed...at worst in OP's case if the figure had been munged on after creation all that's lost is a step of reading it again and using the other alternative of retrieving all handles. A nuisance, perhaps, but since he's starting with a saved figure already, nothing is going to be lost.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationAxes AppearanceCombine Multiple PlotsSubplots
Find more on Subplots in Help Center and File Exchange
Tags
- figure
- subplot
- handles
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office