Matlab ploting in one figure from function

1

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.

Actually script is working fine, is just plots are overlapping with each other.

ZeroVash

Posted 2018-09-08T23:23:36.420

Reputation: 111

Answers

1

The hold command operates on the axes of a figure. Your code produces a figure, but it doesn't contain an axes when you call hold.

Fix this by putting the hold just after your plot command to keep the previuos plots in the same figure.

...
plot(t,y,'color',rand(1,4));
hold on
...

Also, the hold all will be removed in future releases of Matlab, use hold on instead.

JockeR

Posted 2018-09-08T23:23:36.420

Reputation: 21