Return a figures from different functions and subplot them

8 ビュー (過去 30 日間)
Tanvir Hossain
Tanvir Hossain 2020 年 8 月 13 日
コメント済み: Walter Roberson 2020 年 8 月 16 日
I have three functions, these function generates 3 different plots. Now I want to subplot them from my main script.
How can I do it?

回答 (2 件)

David Hill
David Hill 2020 年 8 月 13 日
If your three functions are a, b, c; then your script could be:
subplot(3,1,1);
a;
subplot(3,1,2);
b;
subplot(3,1,3);
c;
  1 件のコメント
Tanvir Hossain
Tanvir Hossain 2020 年 8 月 13 日
I have tried it. but didn't work. here is my function formate:
NB: I want to plot the figure for two time. one is from function itself. Another is from main script with subplot.
function [y, h1] = func1(x)
y = sin(x);
figure();
h1 = plot(x, y)
end

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


Walter Roberson
Walter Roberson 2020 年 8 月 13 日
Your function returns the line handes. If you record them, then you can take each handle and set its Parent property to be the axes that you want the line to appear in. For example,
[y1, h1] = func1(x1);
[y2, h2] = func2(x2);
ax1 = h1.Parent;
ax2 = h2.Parent;
fig1 = ax1.Parent;
fig2 = ax2.Parent;
spax1 = subplot(1,2,1);
h1.Parent = spax1;
spax2 = subplot(1,2,2);
h2.Parent = spax2;
delete(fig1);
delete(fig2);
  2 件のコメント
Tanvir Hossain
Tanvir Hossain 2020 年 8 月 16 日
I tried your code. but it showing the following error.
>> ax1 = h_c.Parent;
>> ax2 = h_r.Parent;
>> fig1 = ax1.Parent;
>> fig2 = ax2.Parent;
>> spax1 = subplot(1,2,1);
>> h_c.Parent = spax1;
Axes cannot be a parent.
Walter Roberson
Walter Roberson 2020 年 8 月 16 日
What is h_c and h_r ? And are you still using the code
function [y, h1] = func1(x)
y = sin(x);
figure();
h1 = plot(x, y)
end
That will return a Chart Line handle in the second output. If h_c is a chart line object, then it should be fine to set spax1 as its parent.

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

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by