How to set a marker at a desired location on a plot
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
absomnia48
le 25 Déc 2016
Modifié(e) : Star Strider
le 26 Déc 2016
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
0 commentaires
Réponse acceptée
Star Strider
le 25 Déc 2016
The easiest way is to define a second vector with integer values, since by inspection only the endpoints of ‘x’ are integers:
x = linspace(-4,4);
xi = min(x) : max(x); % Integer ‘x’ Vector
y = x.*exp(-x.^2);
yi = xi.*exp(-xi.^2);
plot(x,y)
hold on
plot(xi, yi, 'rp', 'MarkerFaceColor','g')
hold off
grid
0 commentaires
Plus de réponses (2)
John BG
le 25 Déc 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
0 commentaires
absomnia48
le 25 Déc 2016
2 commentaires
Star Strider
le 25 Déc 2016
Modifié(e) : Star Strider
le 26 Déc 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.
Voir également
Catégories
En savoir plus sur Data Exploration dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!