Merging multiple graphs in the same tiled layout

39 vues (au cours des 30 derniers jours)
Miguel
Miguel le 21 Sep 2023
Commenté : Voss le 21 Sep 2023
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!

Réponse acceptée

Voss
Voss le 21 Sep 2023
Modifié(e) : Voss le 21 Sep 2023
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 commentaires
Miguel
Miguel le 21 Sep 2023
This worked, thanks so much!
Voss
Voss le 21 Sep 2023
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 21 Sep 2023
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 commentaire
Dyuman Joshi
Dyuman Joshi le 21 Sep 2023
%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)

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by