## how to set a marker at one specific point on a plot (look at the picture)?

### benjamin ma (view profile)

on 27 Feb 2014
Latest activity Commented on by MathWorks Support Team

on 27 Nov 2018

### Mischa Kim (view profile) ### Mischa Kim (view profile)

on 27 Feb 2014
Edited by Mischa Kim

### Mischa Kim (view profile)

on 27 Feb 2014

hold on
plot(x_pos,y_pos,'r*')
to your code. (x_pos,y_pos) is the data point that needs to be marked. So if you have
x = 0:0.1:pi;
y = sin(x);
you could use
plot(x,y)
hold on
plot(x(10),y(10),'r*') % marking the 10th data point of x and y

benjamin ma

### benjamin ma (view profile)

on 27 Feb 2014
Thank you. it works now. but one question, can i use "set" function to mark a plot also?
Matt Kindig

### Matt Kindig (view profile)

on 27 Feb 2014
Sure. First you will need to define the plot object, however. Something like this should work:
h = plot(NaN, NaN, 'r*'); %define the graphics object, but don't render anything.
set(h,'Xdata', x(10), 'YData', y(10)); %mark 10th point
pause(5); %after a few seconds...
set(h, 'XData', x(20), 'YData', y(20)); %mark 20th point instead.

on 6 Dec 2017
Edited by Greg

### Greg (view profile)

on 6 Dec 2017

With 7k views in a month, I'm surprised this hasn't been updated.
Starting in R2016b, there is a MarkerIndices property. Instead of the other answer's suggested:
plot(x,y);
hold on;
plot(x(10),y(10),'r*');
Now simply use:
h = plot(x,y,'MarkerIndices',10);
Move the marker(s) around at any time if you've stored the handle h:
h.MarkerIndices = 5:5:length(x);

#### 1 Comment

MathWorks Support Team

### MathWorks Support Team (view profile)

on 27 Nov 2018
In addition, you can specify the ‘o-‘ line style, which creates a solid line and markers. You can also specify marker properties, such as the face color and edge color.
x = linspace(0,pi,30);
y = sin(x);
p = plot(x,y,'o-','MarkerFaceColor','red','MarkerEdgeColor','red','MarkerIndices',10)

### navi g (view profile)

on 9 Jan 2017

hello, is this marking in plot is possible without writing code, and putting mark in plot in figure editor,
and for sinosoidal curve i have only x data, i dont have y data, but i need to mark on curve, means that i will give x value, based on x value it should place on curve exactly on sinosoidal curve,

Walter Roberson

### Walter Roberson (view profile)

on 9 Jan 2017
For R2014b or later (I would have to check about earlier; I see some references in 2012 time frame) you can use data brushing. Click on the paintbrush in the figure and then you can click on a point to mark it.
For marking a particular location given only the x, then
x_to_mark = SomeSpecificXValue;
all_lines = findobj(gca, 'type', 'line');
number_of_marks = 0;
where_to_mark = [];
for K = 1 : length(all_lines)
this_line = all_lines(K);
this_xdata = get(this_line, 'XData');
if x_to_mark < min(this_xdata) | x_to_mark > max(this_xdata)
continue; %the line does not span that x
end
x_diff = diff(this_xdata);
if isempty(x_diff)
fprintf('skipping line #%d that is single point\n', K);
elseif all(x_diff > 0) | all(x_diff < 0)
%it is monotonic, safe to do interpolation
this_ydata = get(this_line, 'YData');
y_to_mark = interp1(this_xdata, this_ydata, x_to_mark);
number_of_marks = number_of_marks + 1;
where_to_mark(number_of_marks,:) = [x_to_mark, y_to_mark];
else
fprintf('skipping line #%d with unsorted x data\n', K);
end
end
if number_of_marks == 0