Combine figures with subplots in new figure with subplots

6 views (last 30 days)
Hello,
how can I combine 2 figures (h1 and h2) with 2 subplots to one figure (c) with 4 subplots?
h1 = figure
subplot(2,1,1)
subplot(2,1,2)
h2 = figure
subplot(2,1,1)
subplot(2,1,2)
c = figure
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)

Accepted Answer

Mathieu NOE
Mathieu NOE on 30 Nov 2023
hello Paul
like this ?
% Create first figure
hf_sub(1) = figure(1);
hp(1) = uipanel('Parent',hf_sub(1),'Position',[0 0 1 1]);
subplot(2,1,1,'Parent',hp(1));
plot(1:10);
subplot(2,1,2,'Parent',hp(1));
plot(rand(1,100));
% Create second figure
hf_sub(2) = figure(2);
hp(2) = uipanel('Parent',hf_sub(2),'Position',[0 0 1 1]);
subplot(2,1,1,'Parent',hp(2));
histogram(randn(1,1000));
subplot(2,1,2,'Parent',hp(2));
membrane
% Create third figure
hf_sub(3) = figure(3);
hp(3) = uipanel('Parent',hf_sub(3),'Position',[0 0 1 1]);
subplot(2,2,1,'Parent',hp(3));
histogram(randn(1,1000));
subplot(2,2,2,'Parent',hp(3));
membrane
subplot(2,2,3,'Parent',hp(3));
surf(peaks)
subplot(2,2,4,'Parent',hp(3));
plot(-(1:10));
% Create combined figure (horizontal concatenation)
hf_main = figure(4);
npanels = numel(hp);
hp_sub = nan(1,npanels);
% Copy over the panels
for idx = 1:npanels
hp_sub(idx) = copyobj(hp(idx),hf_main);
set(hp_sub(idx),'Position',[(idx-1)/npanels,0,1/npanels,1]);
end
% Create combined figure (vertical concatenation)
hf_main = figure(5);
npanels = numel(hp);
hp_sub = nan(1,npanels);
% Copy over the panels
for idx = 1:npanels
hp_sub(idx) = copyobj(hp(idx),hf_main);
% set(hp_sub(idx),'Position',[0,(idx-1)/npanels,1,1/npanels]); % figures are added from y bottom to top
set(hp_sub(idx),'Position',[0,(npanels-idx)/npanels,1,1/npanels]); % figures are added from y top to bottom
end
  12 Comments
Paul Muster
Paul Muster on 30 Nov 2023
Just a small modification of yours:
% Create first figure
figure;
subplot(2,1,1);
plot(1:10);
subplot(2,1,2);
plot(rand(1,100));
ax1=findobj(gcf,'type','axes'); % get handle to axes of figure
% Create second figure
figure;
subplot(2,1,1);
plot(10:-1:1);
subplot(2,1,2);
plot(rand(1,100));
ax2=findobj(gcf,'type','axes'); % get handle to axes of figure
%%%%%%%%%%
f1 = get(ax1(1),'children'); %get handle to all the children in the figure
f2 = get(ax1(2),'children');
f3 = get(ax2(1),'children');
f4 = get(ax2(2),'children');
%%%%%%%%%%
h3 = figure; %create new figure
s1 = subplot(2,2,1); %create and get handle to the subplot axes
s2 = subplot(2,2,2);
s3 = subplot(2,2,3); %create and get handle to the subplot axes
s4 = subplot(2,2,4);
copyobj(f1,s3); %copy children to new parent axes i.e. the subplot axes
copyobj(f2,s1);
copyobj(f3,s4);
copyobj(f4,s2);

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!