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

조회 수: 19 (최근 30일)
Greg Westbrook
Greg Westbrook 2022년 7월 6일
댓글: Stephen23 2022년 7월 7일
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