Graph plots showing incorrect data. need to get coordinates to the nearest whole number

1 view (last 30 days)
I have this code which when I run and plot points and try to run doesn't display accurate results. theres always a slight difference to where I am pointing. Is there any way I can display and save these coordinates as integers instead?
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
%
clc
figure('WindowButtonDownFcn',@wbdcb)
k=1;
ah = axes('DrawMode','fast');
grid on;
axis ([-10 10 -10 10])%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
% hl = line('XData',0,'YData',0,'Marker','.');
% str=['(',num2str(0),num2str(0),')'];
%
% drawnow
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
end
end
function [x,y,str] = disp_point(ah)
% if k==1
% x=0;y=0;
% k=0;
% else
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
% end
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
  1 Comment
Ashfaq Ahamed
Ashfaq Ahamed on 15 Sep 2015
Someone please try running the code and plotting points, you'll maybe figure out what I'm trying to say. for instance if I try to plot the point at 4,0 it plots it at 3.99 and -1.78e-15. I can't seem to figure out why

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 15 Sep 2015
Figure properties...
The CurrentPoint property contains the coordinates (x, y) of the mouse pointer, measured from the lower left corner of the figure (in units determined by the Units property).
This suggests that you could change the figure Units property to pixels to get integral values.
  2 Comments
Walter Roberson
Walter Roberson on 15 Sep 2015
Consider
oldu = get(ah,'Units');
set(ah,'Units','pixels')
pospix = get(ah,'Position');
ste(ah,'Units',oldu);
Now the width in pixels of the drawing area of the plot will be pospix(3). Your axes([-10 10 -10 10]) indicates that your axes is 10-(-10) = 20 data units across. You then have 20/pospix(3) data units per pixel.
The default figure width is 560 pixels (look at the FactoryDefaultFigurePosition property of the root graphics object to see this.) On my system with my font sizes and those particular labels, this leaves the drawing portion of the axes 434 pixels wide. So each pixel is 20/434 which is just over 0.046 data units per pixel. Therefor when you move by one pixel you move by 0.046 data units. And that is why you cannot mark precisely at whole number of data units -- because the pixels are too wide to be able to pick within 0.01 .
You could always round() the coordinates you get back from disp_point before you use them.

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output 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!