MATLAB: How to connect avg points

plot matrix

Hi, How can I connect avg points?
DAN=[ 19.83 1000.7;
16.72 997.2;
18.47 1012.9;
21.06 987.3;
17.99 1005.1;
18.25 993.6;
18.96 1003.8;
20.73 998.2;
17.97 1005.9;
16.99 1001.2]
x=[2010:2019];
press=(DAN(:,2));
temp=(DAN(:,1));
avg_press=mean(press)
avg_temp=mean(temp)
subplot(2,1,1);
plot(x,press,x,avg_press)
subplot(2,1,2);
plot(x,temp,xavg_temp)
Thanks!

Best Answer

You might want to use the yline function:
DAN=[ 19.83 1000.7;
16.72 997.2;
18.47 1012.9;
21.06 987.3;
17.99 1005.1;
18.25 993.6;
18.96 1003.8;
20.73 998.2;
17.97 1005.9;
16.99 1001.2]
x=[2010:2019];
press=(DAN(:,2));
temp=(DAN(:,1));
avg_press=mean(press)
avg_temp=mean(temp)
subplot(2,1,1);
plot(x,press,'-b')
yline(avg_press,'--b')
subplot(2,1,2);
plot(x,temp,'-r')
yline(avg_temp,'--r')
alternative (if you have an earlier release then R2018b) you could use:
DAN=[ 19.83 1000.7;
16.72 997.2;
18.47 1012.9;
21.06 987.3;
17.99 1005.1;
18.25 993.6;
18.96 1003.8;
20.73 998.2;
17.97 1005.9;
16.99 1001.2]
x=[2010:2019];
press=(DAN(:,2));
temp=(DAN(:,1));
avg_press=mean(press)
avg_temp=mean(temp)
subplot(2,1,1);
plot(x,press,'-b',x,repmat(avg_press,1,numel(x)),'--b')
subplot(2,1,2);
plot(x,temp,'-r',x,repmat(avg_temp,1,numel(x)),'--r')