Round values of one datatip row

3 ビュー (過去 30 日間)
Dominik Deml
Dominik Deml 2022 年 9 月 20 日
編集済み: Adam Danz 2022 年 9 月 20 日
I have the following code:
a = [1.002, 2.006, 3.01, 4.016, 5.022];
b = [2.32, 4.123, 6.864, 8.535, 10.087];
times = [2, 4];
p = plot(a, b);
for i = 1:length(times)
p.DataTipTemplate.DataTipRows(1).Label = "Zeit in (s): ";
p.DataTipTemplate.DataTipRows(2).Label = "Wert: ";
datatip(p, times(i), b(times(i)));
end
I want the 'Zeit in (s)' values to be rounded in the plot. Currently the plot looks like this:
But it should be:
Zeit in (s): 2
Wert: 4.123
and
Zeit in (s): 4
Wert: 8.535
UPDATE:
Running this
...
for i = 1:length(times)
times(i)
p.DataTipTemplate.DataTipRows(1).Label = "Zeit in (s): ";
p.DataTipTemplate.DataTipRows(1).Value = round(a(times(i))).*ones(1, 5);
p.DataTipTemplate.DataTipRows(2).Label = "Wert: ";
datatip(p, a(times(i)), b(times(i)));
end
gives me:
Zeit in (s): 4
Wert: 4.123
and
Zeit in (s): 4
Wert: 8.535
So only the first 4 should be 2.

採用された回答

Adam Danz
Adam Danz 2022 年 9 月 20 日
編集済み: Adam Danz 2022 年 9 月 20 日
If you want to round the x-value, set the format for the first DataTipTextRow of the DataTipTemplate object to '%.0f'. Apply it to the second DataTipTextRow for the y-value.
Demo:
x = rand(1,5)*50;
data = rand(size(x));
h = plot(x, data,'o');
% Set datatip template format for the x-variable
dtt = h.DataTipTemplate;
dtt.DataTipRows(1).Format = '%.0f';
% Create a datatip for demo purposes
disp(x(3))
18.7348
datatip(h,x(3),data(3));
  1 件のコメント
Dominik Deml
Dominik Deml 2022 年 9 月 20 日
Thank you very much!

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

その他の回答 (0 件)

カテゴリ

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by