When I click an image, it doesn't enter its callback
古いコメントを表示
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
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
2022 年 7 月 6 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Develop Apps Using App Designer についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!