Using plot handle to replot a graph

76 ビュー (過去 30 日間)
Ethan Welch
Ethan Welch 2022 年 8 月 13 日
コメント済み: Les Beckham 2022 年 8 月 13 日
In my project, I make an array of graphs. If one of the graphs is interesting, I would like to copy that graph with all of its formatting to a separate figure. I would like to do something like this. I know I could just replot everything, but a lot goes into each graph, and this seems like it should be possible.
x = 0:0.1:10;
y = [sin(x);cos(x);tan(x)];
for i = 1:3;
subplot(1,3,i)
p(i) = plot(x,y(i,:));
end
figure
plot(p(2))

採用された回答

Les Beckham
Les Beckham 2022 年 8 月 13 日
編集済み: Les Beckham 2022 年 8 月 13 日
Read the documentation for the copyobj function. Here is an example:
x = 0:0.1:10;
y = [sin(x);cos(x);tan(x)];
for i = 1:3;
a(i) = subplot(1,3,i);
p(i) = plot(x,y(i,:));
end
f = figure;
copyplot = copyobj(p(2), gca); % copy the plot from subplot 2 to the newly created axis in f

その他の回答 (1 件)

Ethan Welch
Ethan Welch 2022 年 8 月 13 日
編集済み: Ethan Welch 2022 年 8 月 13 日
Thank you for your help. Now I realize I need one step further. I have multiple plots on the same graph and only the last plot is copied over. Is there a way to copy them all at once?
x = 0:0.1:10;
y = [sin(x);cos(x);tan(x)];
for i = 1:3
a(i) = subplot(1,3,i);
hold on
plot(x,y(i,:));
p(i) = plot(x,y(mod(i+1,3)+1,:));
hold off
end
f = figure;
copyplot = copyobj(p(2), gca);
  2 件のコメント
Ethan Welch
Ethan Welch 2022 年 8 月 13 日
Okay, I figured it out.
x = 0:0.1:10;
y = [sin(x);cos(x);tan(x)];
for i = 1:3
a(i) = subplot(1,3,i);
hold on
p(i) = plot(x,y(i,:));
plot(x,y(mod(i+1,3)+1,:))
hold off
end
f = figure;
copyplot = copyobj(a(2).Children, gca);
Les Beckham
Les Beckham 2022 年 8 月 13 日
Great. Exactly what I was going to suggest. That's how to learn: read the docs and experiment until you get the result that you want.

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

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by