How to set a marker at a desired location on a plot
Show older comments
Hi,I'm trying to set a marker at integer values of x.I've used index method(x(3)) but couldn't do it.Because I used linspace function.How do I mark my graph when x is equal to an integer number?Thanks.
x=linspace(-4,4);
y=x.*exp(-x.^2);
plot(x,y)
end
Accepted Answer
More Answers (2)
John BG
on 25 Dec 2016
use a vector index, let it be k, instead of the x reference of the plot.
1.
plot and add a marker
x=linspace(-4,4);
y=x.*exp(-x.^2);
plot(x,y)
hold on
p = plot(x(1),y(1),'o','MarkerFaceColor','red');
2.
now you can simply place the marker on one of the plotted points of y by varying k between 1 and numel(y), for instance:
k=20;
p.XData = x(k);
p.YData = y(k);
or
hold on;
p = plot(x(k),y(k),'o','MarkerFaceColor','red');
hold off;
the figure window also has a marker called Data Cursor.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help, please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
absomnia48
on 25 Dec 2016
2 Comments
Star Strider
on 25 Dec 2016
Edited: Star Strider
on 26 Dec 2016
You are almost there with the ‘%or this one’ version. You only need to add a function handle, here that would be ‘@(x)’:
fx = @(x) x.*exp(-x.^2);
Ymin = fx(fminbnd(fx, -4, 4))
Ymax = fx(fminbnd(@(x) -fx(x), -4, 4))
Ymin =
-428.8819e-003
Ymax =
428.8819e-003
Note that to get the maximum, take the minimum of the negative of the funciton.
Start Strider, Y maxmin are
Ymax=0.42
Ymin=-0.42
while
Xmin=-0.707
Xmax=0.707
because
x=[-4:.00001:4]; y=x.*exp(-x.^2);
min(y)
=
-0.428881942471466
max(y)
ans =
0.428881942471466
find(y==min(y))
=
329290
x(find(y==min(y)))
=
-0.707110000000000
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!