Round values of one datatip row

조회 수: 10 (최근 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));

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by