2
votes

Here are the list of commands I have done to try to place an external figure (both figures listed at http://www.atmos.uw.edu/~akchen0/CERES_Project/) into a subplot slot in an open figure.

subplot(2,2,1);
a = open('blah.fig');
plot(a);

I'd like blah.fig to be subplot 1, but I only get an error message when I try to "plot" a.

Tried the listed solution from the answer, but it didn't print out any of the labels/legends (and a few of the graphs are missing) - the results are roughly shown below.

copyobj(findobj('type','line'),s1)

allows me to copy all the lines fine (though they're distorted a bit). If I want to try copying the legend with

copyobj(findobj(gcf1,'Type','axes','Tag','legend'),s1)

It does not work, and displays the error message "Object axes1 can not be a child of parent axes1". If I use ax1, it displays "invalid handle". Same for the command below:

copyobj(findobj('type','axes'),s1)

http://puu.sh/8S50L.png

Some links I tried but didn't work: http://www.mathworks.com/matlabcentral/answers/92538-how-can-i-copy-an-existing-figure-onto-another-figure-as-a-subplot-using-matlab-7-10-r2010a

Or this: http://www.mathworks.com/matlabcentral/newsreader/view_thread/108304

>> figure_children = get(gcf1,'Children');
children_axes = findall(figure_children,'Type','axes');
>> copyobj(children_axes,s1)
Error using copyobj
Object axes[1] can not be a child of parent axes[1]

allchild does not work either.

>> copyobj(allchild(h1),s1)
Error using copyobj
Object uicontextmenu[1] can not be a child of parent axes[1]

The second reply here works for its own example but not for my example.

1

1 Answers

3
votes

In order to copy MATLAB figure (.fig) files, here are the steps you need to follow.

  1. First open them up with openfig and use the reuse flag. This ensures that you don't reload the figures if they're already open.
  2. Make sure you get the current axes for all these loaded figures.
  3. Make a new figure, then get a handle for each subplot slot you want.
  4. Grab all of the children elements from the handles to the axes of each figure you loaded in from file.
  5. Use copyobj to copy the figures using the children elements from Step #4 into each subplot slot using the subplot handles.

In other words, take a look at this example:

% /// Step #1 and #2
h1 = openfig('test1.fig','reuse'); %// open figure from file
ax1 = gca; % // get handle to axes of figure

h2 = openfig('test2.fig','reuse'); %// open another figure from file
ax2 = gca; % // get handle to axes of the other figure

%// test1.fig and test2.fig are the names of the figure files which you would 
%// like to copy into multiple subplots

% /// Step #3
h3 = figure; %// create new figure
s1 = subplot(2,1,1); % // create and get handle to the subplot axes
s2 = subplot(2,1,2);

% /// Step #4
fig1 = get(ax1,'children'); %// get handle to all the children in the figure
fig2 = get(ax2,'children');

% /// Step #5
copyobj(fig1,s1); %// copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);