```Date: Jan 23, 2013 11:06 PM
Author: Derek Goring
Subject: Re: Single Trendline for multiple datasets

On Thursday, January 24, 2013 11:58:08 AM UTC+13, Charlotte  wrote:> TideMan <mulgor@gmail.com> wrote in message <7c362e36-0ca7-4f58-9c1a-19020b99fc8b@googlegroups.com>...> > > On Thursday, January 24, 2013 10:03:08 AM UTC+13, Charlotte  wrote:> > > > TideMan <mulgor@gmail.com> wrote in message <5ee51095-fef4-4353-8f44-4474a719180f@googlegroups.com>...> > > > > > > > > On Thursday, January 24, 2013 9:24:08 AM UTC+13, Charlotte  wrote:> > > > > > > > > > I have a plot with multiple datasets plotted, and would like to plot on top a single trendline for all the datasets - is there an easy way to do this?> > > > > > > > > > > > > > > > > > 1.  Assemble all the data into two vectors, x and y> > > > > > > > >     x=[x1(:);x2(:);  etc ];> > > > > > > > > 2.  Fit a line to the data using least squares:> > > > > > > > >     coef=[x ones(length(x),1)]\y;> > > > > > > > > 3.  Define the line:> > > > > > > > >     xfit=[min(x) max(x)];> > > > > > > > >     yfit=coef(1)*xfit + coef(2);> > > > > > > > > 4.  Plot> > > > > > > > >     plot(x,y,'bo',xfit,yfit,'r-')> > > > > > > > > > > > > > > > > > > > > > > > thank you very much for your reply - I get an error on the coef line:> > > > > > > > ??? Error using ==> horzcat> > > > > > > > CAT arguments dimensions are not consistent.> > > > > > > > > > > > > > > > Error in ==> Postplotmultiples_CAD at 44> > > > > > > > coef=[x ones(length(x),1)]\y;> > > > > > > > > > > > > > > > fyi - my x vector has multiple instances of the same value> > > > > > > > > > > > > > > > so my y vector will be something like [332.456 345.321 234.543 453.456, etc.]> > > > > > > > and my x will be [1 2 1 2, etc.]> > > > > > > > > > > > > > > > any help much appreciated!> > > > > > Your x is a row vector, whereas ones(length(x),1) is a column vector.> > > You need to make them consistent by transposing (append an apostrophe):> > > coef=[x' ones(length(x),1)]\y';> > > alternatively:> > > coef=[x(:) ones(length(x),1)]\y(:);> > > > it works of course, I am very sorry for not seeing that simple error - thank you so much for your help - of course 'dpb' makes a good point as after this code, I lose the different colours of the different datasets, but his lsline suggestion didn't work - is there an easy way that the separate datasets could remain individually coloured that you can think of?Plot them before concatenating, then plot the fitted line over the top.Of course, you'll have to use:hold on
```