How to overlap plots from different scripts?

178 次查看(过去 30 天)
Hello MATLAB community!
I am trying to overlap two plots from two different scripts in order to illustrate the different best fit lines for the two plots. I am a fairly new MATLAB learner and am unsure how to go about doing this. I tried combining the two scripts into one and following the first plot with "hold on," but that didn't work.
Any suggestions? Perhaps I could save my plot from the first script and upload it into the second script.
Thanks!
  2 个评论
Ameer Hamza
Ameer Hamza 2020-10-5
My guess is that you are calling figure() in your second script, in which case hold on will not work as you are expecting.
Lucia Wagner
Lucia Wagner 2020-10-5
I am trying to now use the copyobj() function... Here is my latest attempt:
p1 = openfig("fig1.fig")
p2 = openfig("fig2.fig")
copyobj(p1,p2)
This is generating a blank figure. Any suggestions?

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-10-5
编辑:Adam Danz 2020-10-5
Look up the documentation for copyobj.
Not only is a new figure generated with the code you shared but a very important error message is produced as well.
Error using copyobj
Figure parent must be the root.
What you want to do is copy the children of axis 1 to axis 2.
Assuming each figure has 1 and only 1 axis,
p1 = openfig("fig1.fig");
p2 = openfig("fig2.fig");
copyobj(get(gca(p1),'Children'), gca(p2));
% ----------1------------ ----2---
% 1: children of axis in figure p1
% 2: axis of figure p2

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-10-5
编辑:Fangjun Jiang 2020-10-5
In the first script or function, do f1=figure and then plot; return f1 if it is a function.
In the second script or function, do figure(f1);hold on and then plot;
If don't want to pass f1, you could add some unique name in the first figure. As long as you can find the figure in the first script, you can then do hold on and plot.
Below is in one script, but it could be split as long as figure f1 is not closed.
f1=figure;
plot(1:10);
f2=figure;
plot(rand(10));
figure(f1);
hold on;
plot(sin(1:0.1:5))

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by