Code covered by the BSD License

### Highlights from Plot average line

4.0
4.0 | 2 ratings Rate this file 30 Downloads (last 30 days) File Size: 10.2 KB File ID: #27134 Version: 1.0

# Plot average line

by

### Jonas (view profile)

plotAverage plots an average line into the data.

File Information
Description

plotAverage adds an average line into plotted data.

By default, plotAverage
- calculates the average along y (for given or estimated points along x)
- plots errorbars with the standard error of the mean
- discards outliers to calculate the robust average
- plots the average line into the data's axes (as opposed to a new figure)

All these options can be changed.

plotAverage returns the handles to the average line and the error bars as well as the x and y data of the line.

Acknowledgements

Average Plot Of Data In A Figure inspired this file.

MATLAB release MATLAB 7.10 (R2010a)
31 May 2016 Edmund Wrigley

### Edmund Wrigley (view profile)

Hi Jonas,
This looks very useful. I have ten blue lines and I want to create an average of the 10 over the top, but when I try and use plotAverage it produces another graph in a new window with a range of different coloured lines on. Is there a way to just get the average line over the top of the original?

Comment only
06 May 2015 Jonas

### Jonas (view profile)

Hi Han,

In this case, I suggest you pass the data used to create the line groups as input to plotAverage.

If you do not have access to the data anymore, but each subgroup has its own color (or e.g. its own tag), you can use "findobj" to gather the line handles and extract x and y data. For example, to plot an average for the blue group:
blueH = findobj(gcf,'color','b');
xData = get(blueH,'xData');
yData = get(blueH,'yData');
xyData = [xData,yData]';
plotAverage(xyData(:));

Comment only
04 May 2015 Han Wang

### Han Wang (view profile)

Hi Jonas,
This is great and really helpful!
Quick question is that what would need to do if I have four subgroups of data in one plot, and each subgroup contains multiples lines that need to be averaged.
Thanks!

16 Nov 2014 juan

### juan (view profile)

24 Feb 2013 Jonas

### Jonas (view profile)

Hi Dafni,

Here's an example:

x = 1:0.1:10;
cmap = jet(10);
% plot some lines
figure,hold on,
for i=1:10,
plot(x,sin(x)+randn(size(x)),...
'color',cmap(i,:));
end
% add average, set robust-option to 0
plotAverage(gca,[],'userobustmean',0)

Comment only
24 Feb 2013 Dafni

### Dafni (view profile)

is there any example on how to use the file plotAverage?

Comment only