Getting average value from matlab plot?
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 开发者_如何学JAVAaverage value of them?
Thank you
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.
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 http://www.thinkextensively.com/misc/stackoverflow/images/matlab2.png
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 http://www.thinkextensively.com/misc/stackoverflow/images/matlab1.png
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.
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.
精彩评论