Problems on subplotting 3 MATLAB figures

2 views (last 30 days)
Jo Wan
Jo Wan on 14 Aug 2015
Answered: Walter Roberson on 14 Aug 2015
Hi all, I used the following coding to subplot 3 figures. The figures are able to open but the subplot only shows the 1st one. I don't know why the 2nd and 3rd figures can't be shown. Please help to solve it. Thank you! For ref, I'm using R2009b Matlab.
% /// Step #1 and #2
h1 = openfig(filename1,'reuse'); %// open figure from file
ax1 = gca; % // get handle to axes of figure
h2 = openfig(filename2,'reuse'); %// open another figure from file
ax2 = get(h2,'children'); % // get handle to axes of the other figure
h3 = openfig(filename3,'reuse'); %// open another figure from file
ax3 = 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
figure(i); %// create new figure
s1 = subplot(3,1,1); % // create and get handle to the subplot axes
s2 = subplot(3,1,2);
s3 = subplot(3,1,3);
% /// Step #4
fig1 = get(ax1,'children');
fig2 = get(ax2,'children');
fig3 = get(ax3,'children');
% /// Step #5
copyobj(fig1,s1); %// copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);
copyobj(fig3,s3);
saveas(gcf,strcat(path,'Subplot\1to3\','AccuRain_1to3_',num2str(Year)))
close(figure(i))

Answers (1)

Walter Roberson
Walter Roberson on 14 Aug 2015
openfig() with 'reuse' brings the given figure to the top, but the top figure is not necessarily the current figure. Even if it does happen to make the figure the current figure, gca does not change just because you change current figures: if you want an axes you need to find it underneath the figure, similar to the way you did for ax2.
h1 = openfig(filename1,'reuse'); %// open figure from file
ax1 = findobj(h1,'type','axes');
The code you are using does not account for the possibility of multiple axes being present in a figure. In particular that can happen if you are using R2014a or earlier and you have a legend. In R2014b and later, legends are not implemented as axes.
Axes can have hidden children -- it happens all the time for labels. To handle those, use findall :
ax1ch = findall(ax1, '-depth', 1, '-not', 'type', 'axes');
copyobj(ax1ch, s1);
Putting this together:
filenames = {filename1, filename2, filename3};
fig_ax = [];
for K = 1 : length(filenames)
fig_h(K) = openfig(filenames{K}, 'reuse');
fig_ax = [fig_ax; findobj(fig_h(K),'-depth',1,'type','axes')]; %not findall
end
num_subplot = length(fig_ax);
for K = 1 : num_subplot
axch = findall(fig_ax(K), '-depth', 1, '-not', 'type', 'axes');
spax = subplot(num_subplot, 1, K);
copyobj(axch, spax);
oldprops = get(fig_ax(K));
props_to_copy = rmfield(oldprops,{'Children','BeingDeleted','CurrentPoint','TightInset','Type','Title','XLabel','YLabel','ZLabel','Position','Parent'});
set(spax, props_to_copy);
end
This code does not attempt to copy legends, and it does not copy the title or xlabel, ylabel, or zlabel . But it does copy things like tick positions
A better job could be done and more easily if it was permitted to create uipanel to hold the plots instead of requiring that they go to subplots.
-----
Note for my own future reference:
ch1 = findall(1,'-depth',1,'-not','type','figure','-not','type','uimenu','-not','type','uitoolbar')

Community Treasure Hunt

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

Start Hunting!