How to extract subplots from a saved figure to copy say, subplot(1,2,1) into new figure

34 views (last 30 days)
I have multiple figures that consist 2 subplots. I want to load the figures, extract each subplot(1,2,1), for example, and put in another figure (of subplots), and each subplot(1,2,2) into another figure (of subplots as well). I don't know how to access the individual subplots within a plot from a loaded figure.
Thanks

Answers (3)

Akshay Kumar
Akshay Kumar on 8 Aug 2018
If you are looking for code to do it, use this Click here

Walter Roberson
Walter Roberson on 12 Sep 2016
The axes created for subplots do not have any property that indicates which subplot combination they were created for. What you will need to do is openfig() to get the handle of the fig, findobj() with 'type', 'axes' to find both axes, then take the one for which the position X coordinate is greater.
  2 Comments
Jorge Rivé
Jorge Rivé on 12 Sep 2016
I'm with you for openfig and findobj...then I'm lost, ;-). not sure what to do with this (subplot's in original figure are actually (2,1,1) and (2,1,2).
Axes with properties:
XLim: [0 1]
YLim: [0 1]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.1300 0.1100 0.7750 0.8150]
Units: 'normalized'
Show all properties
ALim: [0 1]
ALimMode: 'auto'
ActivePositionProperty: 'outerposition'
AmbientLightColor: [1 1 1]
BeingDeleted: 'off'
Box: 'off'
BoxStyle: 'back'
BusyAction: 'queue'
ButtonDownFcn: ''
CLim: [0 1]
CLimMode: 'auto'
CameraPosition: [0.5000 0.5000 9.1603]
CameraPositionMode: 'auto'
CameraTarget: [0.5000 0.5000 0.5000]
CameraTargetMode: 'auto'
CameraUpVector: [0 1 0]
Walter Roberson
Walter Roberson on 13 Sep 2016
fig = openfig('whatever.fig');
ax = findobj(fig, 'type', 'axes'); %should return a vector of length 2
if length(ax) < 2
error('there was only %d visible axes in the figure', length(ax) );
end
axpos = [ax(1).Position; ax(2).Position];
if axpos(1,1) > axpos(2,1)
disp('first axes is to the right of second')
elseif axpos(1,2) < axpos(2,1)
disp('first axes is to the left of second');
else
disp('axes are lined up horizontally');
end
if axpos(1,2) > axpos(2,2)
disp('first axes is above second')
elseif axpos(2,2) < axpos(2,2)
disp('first axes is below second');
else
disp('axes are lined up vertically');
end

Sign in to comment.


Steven Lord
Steven Lord on 12 Sep 2016
If you intend the subplot axes in the destination figure to be in the same sized tiling as in the source figure (so you want subplot axes (m, n, p) from the source figure to be in location (m, n, p) in the destination figure) then once you have their handles use the copyobj function to copy them from the source figure to the destination figure.
If you want the axes in the destination figure to be a different size than the axes in the source figure but you know how large you want the axes to be in the destination, use copyobj to copy the axes then change the Position property of the copied axes.
  3 Comments
x!lef
x!lef on 11 Apr 2017
Did you find a satifying solution for your problem? If so, could you please post it here? I'm facing the same problem. Thank you and all the best Felix
Walter Roberson
Walter Roberson on 11 Apr 2017
As I explained above, subplot() does not record its arguments inside the axes, so the only way to figure out which subplot is which is to compare the Position properties of the axes to find the one you want.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!