MATLAB: How to label the data points with the corresponding index/x-co​ordinate/y​-coordinat​e

coordinatedataindexindicesMATLABpointpointsvalue

How do I label my data points with the corresponding index/x-coordinate/y-coordinate?

Best Answer

The following pieces of code will demonstrate two examples of how this might be done.
This example labels the data points with their corresponding x-coordinate values.
% Define x and y

x = (5:30);
y = sin(x);
% Open a new figure

figure;
% Turn hold on so all the points can

% be plotted individually

hold on;
% Calculation for the amount by which the

% label should be displaced in the 'y'

% direction

lbl_dwn = .1*max(y);
% plot and label the individual points

for i = 1:length(x)
plot(x(i),y(i),'r+');
% Label the points with the corresponding 'x' value
text(x(i),y(i)+lbl_dwn,num2str(x(i)));
end
This example labels the data points with the corresponding index of the element.
% Define x and y
x = (5:30);
y = sin(x);
% Open a new figure
figure;
% Turn hold on so all the points can
% be plotted individually
hold on;
% Calculation for the amount by which the
% label should be displaced in the 'y'
% direction
lbl_dwn = .1*max(y);
% plot and label the individual points
for i = 1:length(x)
plot(x(i),y(i),'r+');
% Label the points with the index
text(x(i),y(i)+lbl_dwn,num2str(i));
end