Merging multiple graphs in the same tiled layout
30 次查看(过去 30 天)
显示 更早的评论
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
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
更多回答(1 个)
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
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 Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!