How to set data cursor to max value programically?
Mostrar comentarios más antiguos
Hello, I want to set data cursor to max value how to do it programically? I used following code but its not working properly. Its how cursor for max value but doesn't set value...
x = 0:pi/20:2*pi;
y = sin(x*0.3)+2*cos(2*x);
[max_val,index] = max(y);
plot(x,y); hold on;
plot(x(index),max_val,'ks','markerfacecolor',[0 0 0]);
text(x(index)-0.5,max_val-0.3,['Max: ', num2str(max_val)])
Respuesta aceptada
Más respuestas (2)
Demo:
fig = figure();
ax1 = subplot(1,2,1);
h1 = plot(magic(5));
datacursormode on
datatip(h1(2), 2, 5);
subplot(1,2,2)
h2 = plot(rand(10), 'sb');
datatip(h2(3),'DataIndex',5);
3 comentarios
yijun guo
el 7 de Jul. de 2022
Hello Adam,
thank you for your code. It helps me a lot. But I didn't understand what h1(2) and h2(3) mean. what's the meaning of bracket with 2 and 3?
thanks in advance
Yijun
h1 = plot(magic(5)) creates 5 handles, one for each column of data
h1 = plot(magic(5))
close % close figure, we don't need it here
h1(2) selects the second line object.
yijun guo
el 11 de Jul. de 2022
thanks a lot
Leandro de Oliveira
el 28 de Sept. de 2019
Editada: Leandro de Oliveira
el 28 de Sept. de 2019
Hi guys, I did a new version because it doesn't worked propoerly to me, the data tip do not get updated, so there it is:
clc; clear all; close all;
%-------------------------------------------------------------------------%
s = tf('s');
%-------------------------------------------------------------------------%
G = (s+40)/((s^2)+(1.2*s)+4);
%-------------------------------------------------------------------------%
lw = 2;
h1 = figure(1);
[mag, fase, w] = bode(G);
hObj = semilogx(w, 20*log10(squeeze(mag)), 'linewidth', lw);
[max_val, index] = max(mag);
cursorMode = datacursormode(h1);
hDatatip = cursorMode.createDatatip(hObj);
pos = [w(index) 20*log10(mag(index))];
set(get(hDatatip, 'DataCursor'), 'DataIndex', index, 'TargetPoint', pos);
set(hDatatip, 'Position', pos);
updateDataCursors(cursorMode);
grid on
%-------------------------------------------------------------------------%

1 comentario
mogilipuri harish
el 20 de Abr. de 2020
how can we add multiple datatips using programically
Categorías
Más información sobre Entering Commands en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
