How to set data cursor to max value programically?

28 ビュー (過去 30 日間)
Rohit Bhoi
Rohit Bhoi 2016 年 4 月 11 日
コメント済み: yijun guo 2022 年 7 月 11 日
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)])

採用された回答

Baltam
Baltam 2016 年 4 月 11 日
編集済み: Walter Roberson 2016 年 4 月 11 日
x = 0:pi/20:2*pi;
y = sin(x*0.3)+2*cos(2*x);
[max_val,index] = max(y);
h =figure; hPlot = plot(x,y);
cursorMode = datacursormode(h);
hDatatip = cursorMode.createDatatip(hPlot);
pos = [x(index) y(index) 0];
set(hDatatip, 'Position', pos)
updateDataCursors(cursorMode)
Is this what you need?
Kind regards,
Baltam
  3 件のコメント
Scotty Neally
Scotty Neally 2020 年 4 月 9 日
I've tried using this code and it works great in version R2019b with a caveat: when I set the position of the datatip it is highly dependant on the figure window size. I noticed that my X-axis value was a few points off and tried to determine whether my index was incorrect. It turns out that if you rescale the figure window before sending the set(hDatatip,'Position',[X Y Z]); command you will get completely different results based on the figure window size at the time....
Perhaps this is covered in the undocumented section of matlab (https://undocumentedmatlab.com/articles/controlling-plot-data-tips)? I haven't had a chance to read through in detail yet.
Thanks!
-Scotty
Scotty Neally
Scotty Neally 2020 年 4 月 28 日
Was just able to solve my problem with a combination of using dummy plot commands to re-focus the datacursor to the correct axes before creating a new datatip. Also, using the Cursor.DataIndex was infinitely more reliable for me instead of using the built-in search function to find the location when "position" is specified:
plot(app.UIAxes2,[],[]); %refocus datacursormode on UIAxes2
plot(app.UIAxes1,[],[]); %refocus datacursormode on UIAxes1
hDatatip.Cursor.DataIndex = 501; %set datatip position based on data index

サインインしてコメントする。

その他の回答 (2 件)

Adam Danz
Adam Danz 2020 年 10 月 29 日
編集済み: Adam Danz 2020 年 10 月 29 日
Update: as of r2019b you can use datatip(__) to programmatically place a data tip.
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 件のコメント
Adam Danz
Adam Danz 2022 年 7 月 7 日
h1 = plot(magic(5)) creates 5 handles, one for each column of data
h1 = plot(magic(5))
h1 =
5×1 Line array: Line Line Line Line Line
close % close figure, we don't need it here
h1(2) selects the second line object.
yijun guo
yijun guo 2022 年 7 月 11 日
thanks a lot

サインインしてコメントする。


Leandro de Oliveira
Leandro de Oliveira 2019 年 9 月 28 日
編集済み: Leandro de Oliveira 2019 年 9 月 28 日
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:
First of all, I get this code HERE.
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_P1_Bode.jpg
  1 件のコメント
mogilipuri harish
mogilipuri harish 2020 年 4 月 20 日
how can we add multiple datatips using programically

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by