MATLAB Answers

Gal Maman

matlab 2019b - bug in data tips accuracy

Gal Maman
さんによって質問されました 2019 年 11 月 17 日 15:59
最新アクティビティ Adam Danz
さんによって 編集されました 2019 年 11 月 18 日 17:43
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?

  0 件のコメント

サインイン to comment.




1 件の回答

Adam Danz
回答者: Adam Danz
2019 年 11 月 17 日 16:49

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).
To change that, you could create a custom update function that specifies the precision to be reported. Here is a working demo below that you can easily adapt to your plotting function.
% Create a plot
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

  3 件のコメント

Gal Maman
2019 年 11 月 18 日 7:49
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
2019 年 11 月 18 日 12:20
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
2019 年 11 月 18 日 13:01
Very helpful, thank you for your answer!

サインイン to comment.

Translated by