matlab 2019b - bug in data tips accuracy

Hi,
I'm using 2019b and I can't get the accurate value of a point in the graph using data tips.
Attached is an example.
Is there a way to fix this?
Thanks,
Gal

 採用された回答

Adam Danz
Adam Danz 2019 年 11 月 17 日
編集済み: Adam Danz 2021 年 8 月 19 日

4 投票

The values reported in the datatips are accurate, judging from the png image. Precision seems to be the issue, not accuracy. Note that your x and y axis ticks are x10^4. The reason the two x-values appear to be 77020 is probably because the data tips are not showing more precise values (ie, 77020.48930).
Here are two method To change the precision of datatip values.
Method 1: custom update function
Adapt this demo to your needs. The custom update function is not called when manually adding datatips with the datatip() function (available in Matlab r2019a and later).
% Create a plot
figure()
ph = plot(magic(5).*7000+rand(1), 'o');
% Specify a custom update function to your data cursor object
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @customDataCursorUpdateFcn, 'Enable', 'On');
% Here's the function that specifies 5 decimal places
function txt = customDataCursorUpdateFcn(~, event)
pos = event.Position;
txt = {sprintf('X: %.5f', pos(1)), sprintf('Y: %.5f', pos(2))};
end % <- may not be needed
Method 2: using DataTipTemplate
Unlike method 1, this method is supported when manually adding datatips with datatip(). Since datatip content differs between objects, you may need to adapt this demo to your needs. The demo changes the x and y values which are assumed to be in the first and second rows of the datatip. This is applied to all line objects in vector ph.
% Create plot
figure()
ph = plot(magic(5).*7000+rand(1), 'o');
% Specify precision of X (first row) and Y (second row) values in dataip
for i = 1:numel(ph)
ph(i).DataTipTemplate.DataTipRows(1).Format = '%.5f'; % X
ph(i).DataTipTemplate.DataTipRows(2).Format = '%.5f'; % Y
end
Show example datatip
datatip(ph(1), ph(1).XData(4), ph(1).YData(4));

5 件のコメント

Gal Maman
Gal Maman 2019 年 11 月 18 日
You are right, I meant precision.
The function works great, thank you!
The x axis is the indices of the vector (integers), I didn't have this problem in Matlab 2019a.. is there a way to set it to default?
Adam Danz
Adam Danz 2019 年 11 月 18 日
編集済み: Adam Danz 2019 年 11 月 18 日
Glad it worked out! I'm not sure there is a way to set the default precision of data tips. At least there wasn't in 2015 and I haven't seen any updates on that. But there's a simple fix for that. Put your custom cursor update function within its own file and every time you want the increase precision of the datatips, you just have to change the UpdateFcn for the figure (which is 1 line of code).
Gal Maman
Gal Maman 2019 年 11 月 18 日
Very helpful, thank you for your answer!
Adam Danz
Adam Danz 2021 年 8 月 19 日
Method 2 added to answer on 19-Aug 2021.
qilin guo
qilin guo 2022 年 9 月 19 日
Thank you! Method 1 helpe me and it also works in MATLAB R2018a. It looks very elegant.

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2019b

質問済み:

2019 年 11 月 17 日

コメント済み:

2022 年 9 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by