www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/DisplayTextAtMinimumAndMaximumValuesOnGraphExample.m

    %% Determine Minimum and Maximum Points and Add Text
% This example shows how to determine the minimum and maximum data points
% on a graph and add text descriptions next to these values.
% 
% Create a plot.

x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
plot(x,y)
%% 
% Find the indices of the minimum and maximum values in |y|. Use the
% indices to determine the (x,y) values at the minimum and maximum points.

indexmin = find(min(y) == y); 
xmin = x(indexmin); 
ymin = y(indexmin);

indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);


%% 
% Add text to the graph at these points. Use |num2str| to convert the |y|
% values to text. Specify the text alignment in relation to the data point
% using the |HorizontalAlignment| property.

strmin = ['Minimum = ',num2str(ymin)];
text(xmin,ymin,strmin,'HorizontalAlignment','left');

strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');