views:

829

answers:

4

Hey,

I have a simple plot which feature a lot of data points, when i have have graph. Is there a way that i can simple click on all these point and allow matlab to give me a average value of them?

Thank you

A: 

Not very clear average of what values you wanted to calculate. I assume, it's y-coordinates.

I would use RBBOX function to select set of points on the plot.

Try this code:

% sample data
data = rand(1,100);
datax = 1:numel(data);

% draw simple plot
plot(data,'.')

% select the points with mouse and get coordinates
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint');    % button down detected
finalRect = rbbox;                   % return figure units
point2 = get(gca,'CurrentPoint');    % button up detected
point1 = point1(1,1:2);              % extract x and y
point2 = point2(1,1:2);
pmin = min(point1,point2);
pmax = max(point1,point2);

% find the data selected and get average of y values
idx = data >= pmin(2) & data <= pmax(2) & datax >=pmin(1) & datax <= pmax(1);
dataAverage = mean(data(idx));

I have to claim large portion of this code is from rbbox documentation.

yuk
+1  A: 

Another option is to use data brush.

Click brush icon on the figure toolbar and make a selection. Then select in menu Tools-Brushing-Create new variable. You can leave default variable name ans. This variable will contain X and Y coordinates of all selected points. Then just run mean(ans(:,2)) to get average of Ys.

yuk
I think you want to say 'brush'.
Jonas
@Jonas: Thanks, corrected.
yuk
+1  A: 

The easiest way if you don't want to do it programmatically would be to use the data brush and statistics.

I used plot(rand(1,200)) to generate my data. After it has plotted go to Tools > Data Statistics. Y-mean is what you are looking for.

alt text

To get the mean of a specific set of data, select the data you want, then in the menu go to Tools > Brushing > Create New Variable . . .. This creates a variable containing the boxed data. To get the mean do mean(ans). The second value in the vector is the Y-mean. alt text

James
If I recall correctly, you don't even need to brush first. You can go directly to Tools->Data Statistics and get the stats for the whole plot.
mtrw
You are absolutely right...brushing actually has no effect on data statistics.
James
A: 

I guess that you want to plot an average (or at least calculate it) from already plotted data.

With plotAverage from the Matlab File Exchange, you can do it quite easily.

%# plot some data
figure
plot(randn(100,5))

%# add the average line at every 5th point
[plotHandles, average] = plotAverage([],5:5:95);

%# and you have a line on the plot, and its handles and data in the workspace.
Jonas

related questions