How to combine 2 graphs from different scripts in another script

14 ビュー (過去 30 日間)
Hannah
Hannah 2023 年 4 月 22 日
コメント済み: Star Strider 2023 年 4 月 22 日
Hello everyone, I am trying to combine 2 graphs, ech made in a different script, in another script. I used the code written underneath. However when I run the code it gives an error that it can't find the figures that I made. Can someone tell me what I did wrong and how to make this work? Thanks in advance!
%graph 1
fg1 = figure;
hold on
plot(Q,h);
xlabel('Debiet[m³/s]');
ylabel('Opvoerhoogte [m]');
%graph 2 in another script
fig2 = figure;
hold on;
curve_head_debiet = plot(G_debiet_int,G_head_int);
curve_head_debie2_2 = plot(n_IP*G_debiet_int,G_head_int);
xlim([0 650]);
ylim([0 70]);
xlabel('Debiet [m³/h]');
ylabel('Head [m]');
%combined graph in another script
pompgrafiek = openfig("fg1.fig");
leidingsgrafiek = openfig("fg2.fig");
copyobj(get(gca(leidingsgrafiek),'Children'), gca(pompgrafiek));

採用された回答

Star Strider
Star Strider 2023 年 4 月 22 日
I cannot get copyobj to copy the data for both figures to one axes. It copies some of the informaton, however not all of it. This could work with subplot plots.
A work-around is to get and the re-plot the 'line' objects —
figure
plot((0:0.01:1), sin((0:0.01:1)*2*pi*2))
grid
xlabel('Debiet[m³/s]');
ylabel('Opvoerhoogte [m]');
savefig('fg1.fig')
figure
plot((0:0.01:1.5), cos((0:0.01:1.5)*2*pi*3))
grid
xlabel('Debiet [m³/h]');
ylabel('Head [m]');
savefig('fg2.fig')
clf
%combined graph in another script
pompgrafiek = openfig("fg1.fig");
pompgrafiek.Visible = 'off';
Ax1 = gca;
leidingsgrafiek = openfig("fg2.fig");
leidingsgrafiek.Visible = 'off';
Ax2 = gca;
f1 = figure; % Copy To 'subplot' Axes
Ax1c = copyobj(Ax1,f1);
Ax2c = copyobj(Ax2,f1);
subplot(2,1,1,Ax1c)
subplot(2,1,2,Ax2c)
Kids1 = pompgrafiek.Children;
Lines1 = findobj(Kids1,'Type','line');
Lgnd1 = findobj(Kids1,'Type','legend');
for k = 1:numel(Lines1)
x1{k} = Lines1.XData;
y1{k} = Lines1.YData;
end
XL1 = Kids1.XLabel.String;
DN1 = Kids1.YLabel.String;
Kids2 = leidingsgrafiek.Children;
Lines2 = findobj(Kids2,'Type','line');
Lgnd2 = findobj(Kids2,'Type','legend');
for k = 1:numel(Lines1)
x2{k} = Lines2.XData;
y2{k} = Lines2.YData;
end
DN2 = Kids2.YLabel.String;
figure % Plot Both On Single Axes
for k = 1:numel(x1)
plot(x1{k}, y1{k}, 'DisplayName',DN1)
end
hold on
for k = 1:numel(x2)
plot(x2{k}, y2{k}, 'DisplayName',DN2)
end
hold off
grid
xlabel(XL1)
legend('Location','best')
.
  2 件のコメント
Hannah
Hannah 2023 年 4 月 22 日
It works, thank you very much!
Star Strider
Star Strider 2023 年 4 月 22 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by