When I click an image, it doesn't enter its callback

28 次查看(过去 30 天)
I'm having difficulty writing an app with App Designer that allows me to click on an image and capture the coordinates of the click and then immediately draw a box around that clicked point.
I create an image and display it with this code fragment (I've left out the user interface and a lot of setup stuff):
% Draw a box around a known point
dispImg = drawBox(dispImg, [TTx TTy], [app.roiBoxLen app.roiBoxLen], [255, 255, 0], 2);
% Insert Timestamp and Distance Text Boxes
ts = strrep(timestamp, '_', ' ');
dispImg = insertText(dispImg, [350,dims(1)-20], ['Range: ',num2str(TargetRange, '%0.2f'), ' nmi'],...
'TextColor','g','FontSize',20, 'BoxColor', 'black', 'BoxOpacity',1,'AnchorPoint','LeftBottom');
dispImg = insertText(dispImg, [50,20], ts,'TextColor','g','FontSize',20, 'BoxColor', 'black','BoxOpacity',1);
dispImg = insertText(dispImg, [200,dims(1)-20], irType,'TextColor','g','FontSize',20, 'BoxColor', 'black','BoxOpacity',1,'AnchorPoint','LeftBottom');
drawnow;
% display the image on app.UIAxes
image(dispImg, 'Parent', app.UIAxes, 'ButtonDownFcn', @app.ImageClicked);
% then do something with the X,Y returned from app.ImageClicked
Then the callback (called "ImageClicked") looks like this
% Button down function: UIAxes
function ImageClicked(app, event)
% Check Matlab version, throw error if prior to r2020b
% Got this from a post on Matlab Central - not sure if needed
assert(~verLessThan('Matlab', '9.9'), 'ginput not supported prior to Matlab r2020b.')
coords = event.IntersectionPoint;
% For now, just display to the command line until I get this callback debugged
disp(coords);
end
The problem is that when I click the image, that callback is never hit.
It throws this error:
Error using ABSViewer/ImageClicked
Too many input arguments.
Error in ABSViewer>@(varargin)app.ImageClicked(varargin{:}) (line 93)
app.myImage.ButtonDownFcn = @app.ImageClicked; %create callback. IMPORTANT: You must use app.
If I include the code as an inline function (or whatever you call it in Matlab when you include a function's code within another function right after you define it as a callback), then it does work.
But I want to understand: why doesn't it hit its callback function like I think it should? This is my first attempt at doing a 'real' app with App Designer and it's very possible I could be misunderstanding how things work.
  2 个评论
Adam Danz
Adam Danz 2022-7-6
Error in ABSViewer>@(varargin)app.ImageClicked(varargin{:}) (line 93)
Your ImageClicked function appears to only support 2 inputs and it looks like the varargin is supplying more than 2 inputs. See line 93, apparently.
Greg Westbrook
Greg Westbrook 2022-7-6
OK, forgive me but I made a mistake in writing my question - trying too many things late in the day yesterday I guess.
The error message that is thrown is actually the following.
Undefined function 'ImageClicked' for input arguments of type 'matlab.graphics.primitive.Image'.
Error while evaluating Image ButtonDownFcn.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2022-7-6
编辑:Adam Danz 2022-7-7
Try this:
image(dispImg, 'Parent', app.UIAxes, 'ButtonDownFcn', @(src,event)ImageClicked(app,src,event));
and then define the callback function:
function ImageClicked(app, src, event)
Additionally, if the ButtonDownFcn is a proprety of an app component and if the ImageClicked function is a method in your app, this is a better approach (see documentation):
image(dispImg, 'ButtonDownFcn', @app.ImageClicked) % app is your app object
and then define the callback function:
function ImageClicked(app,src,event)
  7 个评论
Adam Danz
Adam Danz 2022-7-7
@Greg Westbrook, in App Designer, the app object is typically passed into the first argument. See some examples in the documentation for Callbacks in App Designer. This is optional for your ButtonDownFcn, in fact, you don't even need to pass it in at all if you're not using it, but if you do include it, you can decide whether to follow the standard in App Designer where the app is passed in arg 1 or the standard that additional arguments are passed into callbacks after arg 2.
I'll update my answer to include a third, and perhaps the best, option.
Stephen23
Stephen23 2022-7-7
"the app object is typically passed into the first argument.."
Aah, I did not realize that once again, consistency was thrown to the wind. Thank you for the information!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by