Merging multiple graphs in the same tiled layout

35 ビュー (過去 30 日間)
Miguel
Miguel 2023 年 9 月 21 日
コメント済み: Voss 2023 年 9 月 21 日
Hi there, I'm attempting to merge a couple of graphs saved in the same tiled layout, but I'm not sure if that's possible to be honest. Here's what I mean:
%Meaningless values for graphs
a = 1:1:100
b = 5:5:500
c = 2:2:200
d = 400:-4:4
%Creating first figure w/ 2 graphs + saving
figure();
t = tiledlayout(1,2)
nexttile(t)
plot(a,b)
nexttile(t)
plot(b,a)
saveas(figure(1),'Fig1.fig')
%Creating second figure w/ 2 graphs + saving
figure()
t = tiledlayout(1,2)
nexttile(t)
plot(c,d)
nexttile(t)
plot(d,c)
saveas(figure(2),'Fig2.fig')
If I wanted to merge the four saved graphs into two in the same tiled layout (2x1) how might I go about doing that? Specifically, I'd like it where tile 1 is a,b & c,d on the same graph, and tile 2 is b,a & d,c on the same graph.
I've figured out that you can use the following code to make a combination just of the last graph in the layout, but this is somewhat unhelpful:
h1 = hgload('Fig1.fig')
h2 = hgload('Fig2.fig')
figure()
h(1) = subplot(1,1,1)
copyobj(allchild(get(h1,'CurrentAxes')),h(1));
copyobj(allchild(get(h2,'CurrentAxes')),h(1));
%This would show b,a & d,c on the same graph
I think I could reasonably do this without using any tiled layouts, saving each plot as a new figure, and then combining them in a tiled layout afterwards. Because of the amount of graphs I'm working with and how my figures are already saved as tiled, though, I'd like to see if anyone has any ideas on how to do it another way. Thank you!

採用された回答

Voss
Voss 2023 年 9 月 21 日
編集済み: Voss 2023 年 9 月 21 日
I gather you have figures already saved, whose tiles you want to combine in the way you describe.
%Meaningless values for graphs
a = 1:1:100;
b = 5:5:500;
c = 2:2:200;
d = 400:-4:4;
%Creating first figure w/ 2 graphs + saving
f1 = figure();
t = tiledlayout(1,2);
nexttile(t)
plot(a,b,'b')
title('a,b')
nexttile(t)
plot(b,a,'g')
title('b,a')
saveas(f1,'Fig1.fig')
%Creating second figure w/ 2 graphs + saving
f2 = figure();
t = tiledlayout(1,2);
nexttile(t)
plot(c,d,'r')
title('c,d')
nexttile(t)
plot(d,c,'m')
title('d,c')
saveas(f2,'Fig2.fig')
% openfig is recommended over hgload
h1 = openfig('Fig1.fig','invisible');
t1 = get(h1,'Children'); % tiledlayout object
ax1 = get(t1,'Children'); % axes (both of them) in figure h1
h2 = openfig('Fig2.fig','invisible');
t2 = get(h2,'Children'); % tiledlayout object
ax2 = get(t2,'Children'); % axes (both of them) in figure h2
% new figure and tiledlayout to put stuff into:
f_new = figure();
t_new = tiledlayout(2,1);
% Children are listed in reverse order of creation (i.e., newest first),
% so ax1(2) is the left axes in figure h1 and ax2(2) is the left axes in figure h2
ax1_new = nexttile();
ch = get([ax1(2) ax2(2)],'Children'); % lines (and whatever else) contained in those 2 axes
ch = vertcat(ch{:});
copyobj(ch,ax1_new); % copy them to the new axes
% ax1(1) is the right axes in figure h1 and ax2(1) is the right axes in figure h2
ax2_new = nexttile();
ch = get([ax1(1) ax2(1)],'Children'); % lines (and whatever else) contained in those 2 axes
ch = vertcat(ch{:});
copyobj(ch,ax2_new); % copy them to the new axes
delete([h1 h2]) % delete the invisible figures
  2 件のコメント
Miguel
Miguel 2023 年 9 月 21 日
This worked, thanks so much!
Voss
Voss 2023 年 9 月 21 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2023 年 9 月 21 日
If I understand what you mean, you just need to use the hold function (just after nexttile) to retain plots, rather than replacing them.
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 21 日
%Meaningless values for graphs
a = 1:1:100;
b = 5:5:500;
c = 2:2:200;
d = 400:-4:4;
%Creating first figure w/ 2 graphs + saving
figure();
t = tiledlayout(1,2);
nexttile(t)
plot(a,b)
hold on
plot(c,d)
nexttile(t)
plot(b,a)
hold on
plot(d,c)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxes Appearance についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by