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 CenterFile Exchange에서 Creating, 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