MATLAB Answers

How can I insert my MATLAB figure (.fig) files into multiple subplots?

785 views (last 30 days)
I have two MATLAB figure (.fig) files which I would like to insert into the subplots of a new figure.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Sep 2010
In order to copy MATLAB figure (.fig) files into multiple subplots use the following commands:
h1 = openfig('test1.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
h2 = openfig('test2.fig','reuse');
ax2 = gca;
% test1.fig and test2.fig are the names of the figure files which you would % like to copy into multiple subplots
h3 = figure; %create new figure
s1 = subplot(2,1,1); %create and get handle to the subplot axes
s2 = subplot(2,1,2);
fig1 = get(ax1,'children'); %get handle to all the children in the figure
fig2 = get(ax2,'children');
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);
For additional information on the COPYOBJ function, refer to the following documentation:
Attached are 2 scripts which will demonstrate how to place multiple FIG files into different subplots.
The file 'savfigs.m' creates 2 figures and saves them into the current working directory. Please run this file first.
The file 'copyaxes.m' opens these figures and then copies them to the different subplot axes.

  3 Comments

LIU
LIU on 16 Jan 2014
Hello, all.
I just tried subplotting two graphs with 4 y-axis in a figure by using the commands suggeted above. But only get the result as follows:
Coul you please kindly help correct the codes attached below to get it come out right? Thank you in advance.
The funtion to plot multiple y-axis is:
function [ax,hlines] = plotyyyy(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
%PLOTY4 Extends plotyy to include a third and fourth y-axis
%
% Syntax: [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels)
%
% Inputs: x1,y1 are the xdata and ydata for the first axes' line
% x2,y2 are the xdata and ydata for the second axes' line
% x3,y3 are the xdata and ydata for the third axes' line
% x4,y4 are the xdata and ydata for the fourth axes' line
% ylabels is a 4x1 cell array containing the ylabel strings (optional)
%
% Outputs: ax - 4x1 double array containing the axes' handles
% hlines - 4x1 double array containing the lines' handles
%
% ylabels{3} = 'Third y-label';
% See also Plot, Plotyy
% Denis Gilbert, Ph.D.
% Check inputs
msg=nargchk(8,9,nargin);
error(msg);
% Create figure window
figure('units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');
%Plot the first two lines with plotyy
[ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2);
cfig = get(gcf,'color');
pos = [0.1 0.1 0.65 0.8];
offset = pos(3)/24;
%Reduce width of the two axes generated by plotyy
pos(1) = pos(1) + offset;
pos(3) = pos(3) - offset;
set(ax,'position',pos);
%Determine the position of the third/fourth axes
pos3 = [pos(1) pos(2) pos(3)+offset pos(4)];
pos4 = [pos(1) - offset pos(2) pos(3)+offset pos(4)];
%Determine the proper x-limits for the third and fourth axes
scale3 = pos3(3)/pos(3);
scale4 = pos4(3)/pos(3);
limx1 = get(ax(1),'xlim');
limx3 = [limx1(1) limx1(1)+scale3*(limx1(2)-limx1(1))];
limx4 = [limx1(2)-scale4*(limx1(2)-limx1(1)) limx1(2)];
%Create ax(3) & ax(4)
ax(3) = axes('Position',pos3,'box','off',...
'Color','none','XColor',cfig,'YColor','r',...
'xtick',[],'xlim',limx3,'yaxislocation','right');
ax(4) = axes('Position',pos4,'box','off',...
'Color','none','XColor',cfig,'YColor','k',...
'xtick',[],'xlim',limx4,'yaxislocation','left');
%Plot x3,y3,x4,y4
hlines(3) = line(x3,y3,'Color','r','LineStyle','--','Parent',ax(3));
hlines(4) = line(x4,y4,'Color','k','LineStyle','-.','Parent',ax(4));
%Put ax(2) on top;
axes(ax(2));
%Set y-labels;
if nargin==9
set(cell2mat(get(ax,{'ylabel'})),{'String'},{ylabels{:}}');
end
The example figure is :
x = 0:10;
y1=x; y2=x.^2; y3=x.^3; y4=x.^4;
[ax,hlines] = plotyyyy(x,y1,x,y2,x,y3,x,y4);
saveas(gcf,'fig1'), 'fig')
By using the codes in this post, I did:
h1 = openfig('fig1.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
h2 = openfig('fig1.fig','reuse');
ax2 = gca;
% test1.fig and test2.fig are the names of the figure files which you would % like to copy into multiple subplots
h3 = figure; %create new figure
s1 = subplot(2,1,1); %create and get handle to the subplot axes
s2 = subplot(2,1,2);
fig3 = get(ax1,'children'); %get handle to all the children in the figure
fig4 = get(ax2,'children');
copyobj(fig3,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig4,s2);
Kimberley Owen
Kimberley Owen on 17 Aug 2018
The base code provided by the support team is not working for me in 2016a. It opens a figure with two subplots, but will not copy any objects onto it.
It says "Subscripted assignment dimension mismatch."
eitan asher
eitan asher on 16 Mar 2020 at 13:54
Didn't work for me owith MATLAB 2019a.
This is what I tried to do with one subplot (justto check if it's working)
figname = fig_file.name;
i=1;
h1 = openfig(figname, 'reuse');
ax1 = gca;
h3 = figure;
s1 = subplot(5,2,i)
fig1 = get(ax1,'children');
copyobj(h1,s1);
and I got:
Error using copyobj
Axes cannot be a parent.

Sign in to comment.

More Answers (1)

su yung-chih
su yung-chih on 14 Aug 2017
For some reason, MATLAB may not be able to show the figure in short time. So, user had better add some time-delay after "ax = gac", or the subplot may repeat the same result.
if true
% code
end
ax = gca; pause(2)

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags

Products


Release

R2008a