Use sprintf in callback function to get an output variable

Hello,
I've got a very simple callback function 'countclicks', which displays the type of click (normal,alt,extend or open) and its coordinates in the command window when a mouse click is performed on my figure. How can I use sprintf to get the type of click and the associated coordinates in a seperate variable?
Thank you!
function countclicks(gcbo,eventdata,handles)
disp(get(gcf,'SelectionType'))
disp(get(gca,'CurrentPoint'))
end

回答 (1 件)

Arthur Roué
Arthur Roué 2020 年 8 月 5 日

0 投票

function countclicks(gcbo,eventdata,handles)
strSelectionType = get(gcf,'SelectionType');
XY = get(gca,'CurrentPoint');
sprintf('Type : %s, X = %d / Y = %d', strSelectionType, XY(1), XY(2))
end

3 件のコメント

Sam
Sam 2020 年 8 月 5 日
Thank you! But how exactly can I update the last performed click immediately? Let's say I click left on my figure at a given point in time, is there a way to immediately get a variable which captures this last click?
Steven Lord
Steven Lord 2020 年 8 月 5 日
Callback functions do not return any output arguments. So where do you want to "get a variable" that captures the click location?
I think you may want to use the ginput function.
Arthur Roué
Arthur Roué 2020 年 8 月 5 日
You can store thoses values in variable shared with the GUI main function and its nested functions.
function myGUI()
% Create a local variable shared with nested function
XY = []
% Creat an axe with a callback
hAxe = axes(.., 'ButtonDownFcn', @countclicks)
function countclicks(src, evt)
strSelectionType = get(src.Parent,'SelectionType'); % src.Parent = figure handme
XY = get(src,'CurrentPoint');
sprintf('Type : %s, X = %d / Y = %d', strSelectionType, XY(1), XY(2))
end
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

Sam
2020 年 8 月 5 日

コメント済み:

2020 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by