Can't get or show every integer or pixel value of axes
5 views (last 30 days)
Show older comments
In MATLAB 2022a, App Designer (see attached file), I want to get, show or mark (with a '+') with the mouse cursor, any integer pixel location in an axes or on an image in the axes. The WindowButtonMotionFcn is used to get and show in real-time, the current x,y coordinate position of the mouse cursor in the axes (or on the image) and ButtonDownFcn is used to mark with a '+' two mouse-clicked pixel positions so as to measure the distance between them. Since WindowButtonMotionFcn is defined for the figure, the real-time display of coordinate poitions is limited to display only when the mouse cursor is on the axes (or the image). I want/need pixel values (i.e., integers) to be displayed and I need to be able to access every or any pixel value of the axes (or image in the axes). The axes and the image are 640 x 480 and the axes Position width of 640 and height of 480 and XLims [0 640] and YLims [0 480] are defined accordingly in the StartUpFcn.
The WindowButtonMotionFcn and the ButtonDownFcn return floating point numbers which need to be rounded to integers. This results in one or more of the following problems:
- The last horizontal edge x-coordinate still on the axes is 479.4894 which when rounded is 479 and the very next, adjacent edge coordinate is 480.5106 which when rounded is 481 and is therefore outside of the defined axes (or image). The result is that coordinate positions 480 and 481, both of which should still be on the axes (or image), are not available on the axes that is defined to be 480 pixels wide. This same error occurs for the y-axis coordinates.
- This same error occurs at certain points within the axes (or image) when one floating point value is xx.49.. and the very next adjacent value is xx+1.51...Rounding these gives xx in the first case and gives xx+2 in the second case, but the intervening pixel (or integer value) of xx+1 is never available.
- If ceil() rather than round() is used to convert from floating point to integer, one or more of the same general problems occur (the full width and height are not available and/or some integer values within the axes are not available).
This behavior can be observed in the attached app by carefully positioning the mouse cursor at the very bottom edge or the very top edge (or left edge and right edge) of the axes or by carefully moving the mouse within the image where one floating point value is xx.49... and the next adjacent value is xx+1.51...
I've tried to modify the defined the edge/size of the axes (or image) in various ways (see lines 208-210) but these attempts may fix one error, but generate a similar error elsewhere.
In the app, the x and y floating point coordinate values obtained in WindowButtonMotionFcn are uncommented so they write to the MATLAB command window in real-time as the mouse is moved so these raw floating point x,y coordinate values can be seen.
How does MATLAB define, or what is it that defines the floating point x,y coordinate positions returned by WindowButtonMotionFcn?
Thanks in advance for any help with this.
0 Comments
Answers (1)
Debadipto
on 8 Jun 2023
Hi AG,
MATLAB defines the floating-point coordinates returned by WindowButtonMotionFcn based on the position of the cursor. When the mouse is over the axes, the WindowButtonMotionFcn returns the floating-point values of the mouse cursor position with respect to the axes. These values are relative to the lower-left corner of the axes. The floating-point values represent the fraction of the axes size, where the bottom-left corner of the axes is [0, 0] and the top-right corner is [1, 1]. The WindowButtonMotionFcn function does not know the pixel size of the axes, so it returns floating-point values.
In order to get integer pixel values from the floating-point coordinates, the axes size (in pixels) needs to be multiplied by the floating-point values, and the result needs to be rounded to the nearest integer. This conversion may lead to the issues with missing integer pixel values as described by you.
To solve this issue, one possible solution is to add a small buffer around the axes that is not shown to the user, so that the floating-point values of the edge of the axes are not exactly on the edge of the axes.
Another option is to increase the size of the figure to include the edges of the axes in the window, then modify the mouse pointer position to be relative to the axes by subtracting the axes position from the pointer position.
Regards,
Debadipto Biswas
0 Comments
See Also
Categories
Find more on Line Plots 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!