MATLAB Answers

Incorrect datatip location on interpolated data

조회 수: 1(최근 30일)
Sander
Sander 2021년 6월 21일
편집: Sander 2021년 6월 21일
I am trying to automatically add datatips to my data, but have found some strange behaviour that I do not know how to solve.
I have reduced it to the following example:
figure;
hPlot = plot(1:10, 10:-1:1);
x = 1.58;
y = interp1(hPlot.XData, hPlot.YData, x); % Get y-coordinate at given x.
hDataTip = datatip(hPlot, x, y, 'SnapToDataVertex', 'off'); % Create the datatip
fprintf('Desired: x = %.2f, y = %.2f\nRealized: x = %.2f, y = %.2f\n', x, y, hDataTip.X, hDataTip.Y);
Which gives the following output:
Desired: x = 1.58, y = 9.42
Realized: x = 2.58, y = 8.42
Any x-coordinate that would round up to the next integer gives this strange result.
The problem can also be clearly seen when animating:
figure; hold on;
hPlot = plot(1:10, 10:-1:1);
hMarker = scatter(1,1, 'ro');
hDataTip = datatip(hPlot, 'SnapToDataVertex', 'off');
for(x = 1:0.04:5)
y = interp1(hPlot.XData, hPlot.YData, x); % Get y-coordinate at given x.
hDataTip.X = x;
hDataTip.Y = y;
hMarker.XData = x;
hMarker.YData = y;
drawnow;
pause(0.01);
end
Which at one point gives the following:
What can I do to make the datatip appear at the location I desire?

채택된 답변

Joseph Cheng
Joseph Cheng 2021년 6월 21일
편집: Joseph Cheng 2021년 6월 21일
so you can see what is happening if you plot it as just markers in this animated example:
figure; hold on;
hPlot = plot(1:10, [1:10]);
hMarker = scatter(1,1, 'ro');
hDataTip = datatip(hPlot, 'SnapToDataVertex', 'off');
n=1;
for(x = 1:0.04:5)
y = interp1(hPlot.XData, hPlot.YData, x); % Get y-coordinate at given x.
hDataTip.X = x;
hDataTip.Y = y;
hMarker.XData = x;
hMarker.YData = y;
drawnow;
pause(0.0001);
data(n,:)=[x y hDataTip.X hDataTip.Y];
% disp([x y hDataTip.X hDataTip.Y])
n=n+1;
end
figure,plot(data(:,1),data(:,3));xlabel('desired x val'),ylabel('datatip x val')
where the marker wants to snap to the nearest point in the specified hPlot input line given in the hDataTip creation. i can't quite see quickly what it is wanted to do but when you plot it as a line instead of using just the marker. Seems a bit inconsistent for the 1~2 min i looked at it and trying to compare the extracted by some rounded integer value + keeping the decimal values. as you can see in the desired versus data tip x values at 1.5 the values jump to 2.5
to get around this you should specify the interpolated marker as where you want to put the data tip against:
figure; hold on;
hPlot = plot(1:10, 1:10,'o');
hMarker = scatter(1,1, 'ro');
hDataTip = datatip(hMarker, 'SnapToDataVertex', 'off');
for(x = 1:0.04:5)
y = interp1(hPlot(1).XData, hPlot(1).YData, x); % Get y-coordinate at given x.
hDataTip.X = x;
hDataTip.Y = y;
hMarker.XData = x;
hMarker.YData = y;
drawnow;
pause(0.1);
disp([x y])
end
1 1 1.0400 1.0400 1.0800 1.0800 1.1200 1.1200 1.1600 1.1600 1.2000 1.2000 1.2400 1.2400 1.2800 1.2800 1.3200 1.3200 1.3600 1.3600 1.4000 1.4000 1.4400 1.4400 1.4800 1.4800 1.5200 1.5200 1.5600 1.5600 1.6000 1.6000 1.6400 1.6400 1.6800 1.6800 1.7200 1.7200 1.7600 1.7600 1.8000 1.8000 1.8400 1.8400 1.8800 1.8800 1.9200 1.9200 1.9600 1.9600 2 2 2.0400 2.0400 2.0800 2.0800 2.1200 2.1200 2.1600 2.1600 2.2000 2.2000 2.2400 2.2400 2.2800 2.2800 2.3200 2.3200 2.3600 2.3600 2.4000 2.4000 2.4400 2.4400 2.4800 2.4800 2.5200 2.5200 2.5600 2.5600 2.6000 2.6000 2.6400 2.6400 2.6800 2.6800 2.7200 2.7200 2.7600 2.7600 2.8000 2.8000 2.8400 2.8400 2.8800 2.8800 2.9200 2.9200 2.9600 2.9600 3 3 3.0400 3.0400 3.0800 3.0800 3.1200 3.1200 3.1600 3.1600 3.2000 3.2000 3.2400 3.2400 3.2800 3.2800 3.3200 3.3200 3.3600 3.3600 3.4000 3.4000 3.4400 3.4400 3.4800 3.4800 3.5200 3.5200 3.5600 3.5600 3.6000 3.6000 3.6400 3.6400 3.6800 3.6800 3.7200 3.7200 3.7600 3.7600 3.8000 3.8000 3.8400 3.8400 3.8800 3.8800 3.9200 3.9200 3.9600 3.9600 4 4 4.0400 4.0400 4.0800 4.0800 4.1200 4.1200 4.1600 4.1600 4.2000 4.2000 4.2400 4.2400 4.2800 4.2800 4.3200 4.3200 4.3600 4.3600 4.4000 4.4000 4.4400 4.4400 4.4800 4.4800 4.5200 4.5200 4.5600 4.5600 4.6000 4.6000 4.6400 4.6400 4.6800 4.6800 4.7200 4.7200 4.7600 4.7600 4.8000 4.8000 4.8400 4.8400 4.8800 4.8800 4.9200 4.9200 4.9600 4.9600
5 5
  댓글 수: 1
Sander
Sander 2021년 6월 21일
I was hoping I was using the datatip method incorrectly, but it seems I'll have to use an inelegant workaround.
Thank you for your answer and detailed investigation.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by