Matlab multiple ploting in one figure from function

I wrote a function that plot a function y(t) using 4 input argument.
function plot_me_n1(A,B,m1,m2)
t = linspace(0,10,10/0.01);
y=A*exp(-m1*t) - B*exp(-m2*t);
plot(t,y,'color',rand(1,4));
title('equation', 'fontsize', 10);
ylabel('y(t)');
xlabel('t');
end
Now I'm creating another function that pass to plot_me_n1 function multiple variable to create multiple plots.
figure
hold all
A=[-8,8,-8];
B=[9,-9,-9];
m1=-3;
m2=-4;
arrayfun(@(a,b) plot_me_n1(a,b,m1,m2),A, B);
hold off
The problem is that it display only the last plot, while I'm trying to achieve to display multiple plots at the same time. Important to mention, I cant move plot() into outside the function because I want to keep plot_me_n1 function possible to work by itself not dependently on other scripts. So how to make possible to display all plots at the same time in one figure? Any refactoring comments on how to make those code better is welcome. Thanks.

4 件のコメント

Geoff Hayes
Geoff Hayes 2018 年 9 月 9 日
Alex - when I run your above code, there are three lines drawn/plotted on your axes so multiple plots do appear. There was a bug (when running with my version of MATLAB) with the line
plot(t,y,'color',rand(1,4));
with error
Error using plot
Color value must be a 3 element numeric vector
Error in plot_me_n1 (line 4)
plot(t,y,'color',rand(1,4));
Error in @(a,b)plot_me_n1(a,b,m1,m2)
So I just used the following instead
plot(t,y,'color',rand(1,3));
Alex Mike
Alex Mike 2018 年 9 月 9 日
@geoff Hayes Unfortunately It is not sowing in one figure, running function for each A and B I got following pictures:
Now if when tried to run the script I got this:
As You might see it didn't not print all plots at the same figure. I think it might be useful to change 'color' attribute without random color, just that he print different color each time.
Geoff Hayes
Geoff Hayes 2018 年 9 月 10 日
It looks from your first three images, that there is overlap between the second two images and the first. Can you confirm this? I was able to verify that there were three lines/plots in the my output when running the script...
Alex Mike
Alex Mike 2018 年 9 月 10 日
actually I just realize it, You are right, there are overlap.... Thank you so much...

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

回答 (1 件)

Daksh
Daksh 2023 年 2 月 2 日

0 投票

There seems to be a direct overlap of graph lines in plot figures. The simplest workaround is to display the lines in different colors and you should be able to differentiate all output plots:
plot(t,y,'color',rand(1,3));
Hope it helps!

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2018 年 9 月 9 日

回答済み:

2023 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by