Get pixel location in UIAxes within app designer

4 次查看(过去 30 天)
In my app designer, I have a UIAxes to display an image. I also have an On/Off switch by the image. When the switch is On, I could click a pixel in the image to return the pixel coodinates in the codes for further calcualtion. When the switch is Off, click on the image won't return anything.
I'm stuck at connecting the swtich and coodinate picker. I also don't have a reliable solution to return pixel coodinates. Currently I'm following the example here,
roiPOC = drawpoint(app.UIAxes); %Use Mouse To Select a point ROI
PosPOC=round(get(roiPOC,'Position')); %Extract Coordinates of the point ROI
delete(roiPOC); %Delete the point ROI Marker
drawnow; % Force figure update
but above codes are not stable, sometimes the point won't be deleted. Any thoughts please? I'm using Matlab 2020b.

回答(1 个)

Rahul
Rahul 2025-6-25
Hi Kyle,
I understand you're working in MATLAB App Designer (R2020b) to implement a pixel coordinate picker on a 'UIAxes', which is enabled only when a switch is turned "On".
The current approach utilizes 'drawpoint' to capture clicks, which are not entirely stable, particularly with point deletion and figure responsiveness.
To make this interaction more reliable, you can use the 'ButtonDownFcn' callback on the image object rather than relying on 'drawpoint' which is more optimized towards freehand ROI workflows. This approach gives you more direct control and integrates smoothly with an On/Off switch.
Here is a pattern you can try incorporating in the existing App Designer code:
  • Loading and displaying the image with 'ButtonDownFcn' callback:
% Example in startupFcn or image display function
imgHandle = imshow(yourImageData, 'Parent', app.UIAxes);
imgHandle.ButtonDownFcn = @(src, event) imageClickCallback(app, src, event);
  • Creating a separate function in the 'imageClickCallback' method:
function imageClickCallback(app, src, event)
if strcmp(app.ToggleSwitch.Value, 'On')
cp = app.UIAxes.CurrentPoint;
x = round(cp(1,1));
y = round(cp(1,2));
% Use x, y for further processing
disp(['Selected pixel: (' num2str(x) ', ' num2str(y) ')']);
end
end
This method checks whether the switch is "On" and then captures the click location from the 'CurrentPoint' of the UIAxes. It avoids the overhead of 'drawpoint' and gives consistent results with pixel selection.
  • Ensure that the image or axes has 'PickableParts' set to 'all' and 'HitTest' set to 'on', which is true by default when using 'imshow'.
  • If you are layering other elements on the axes, ensure that the image remains the bottom layer and is receiving clicks.
To know more about the usage of various functions and parameters used in the above-mentioned code snippets, you can refer to the following documentation links:
  1. 'imshow' function: https://www.mathworks.com/help/images/ref/imshow.html
  2. 'CurrentPoint' property:https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#btwpyy7-1_sep_shared-CurrentPoint:~:text=CurrentPoint%20%E2%80%94%20Location%20of%20mouse%20pointer%0ARead%2Donly%3A%202%2Dby%2D3%20array
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by