Round values of one datatip row

4 views (last 30 days)
Dominik Deml
Dominik Deml on 20 Sep 2022
Edited: Adam Danz on 20 Sep 2022
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.

Accepted Answer

Adam Danz
Adam Danz on 20 Sep 2022
Edited: Adam Danz on 20 Sep 2022
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));

More Answers (0)

Categories

Find more on Axes Appearance 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!