Round values of one datatip row

5 vues (au cours des 30 derniers jours)
Dominik Deml
Dominik Deml le 20 Sep 2022
Modifié(e) : Adam Danz le 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.

Réponse acceptée

Adam Danz
Adam Danz le 20 Sep 2022
Modifié(e) : Adam Danz le 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));
  1 commentaire
Dominik Deml
Dominik Deml le 20 Sep 2022
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Axes Appearance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by