Create a Button in a figure which activates another function

Actually I use the 'getpts' function a lot in one of my scripts since I need the user to specify some points in an image. Sometimes the image might have a big resolution, so it would be nice to zoom in before choosing the points (the original 'getpts' function doesn't allow zooming). I read on another forum about the idea to impement a button in the opened figure which starts 'getpts' upon pressing it. With this option the user can zoom into the part of the image where he/she wants to specify points. I wrote a function which does exactly what I want it to do, but there are no output arguments given by it. Can someone help me to spot my mistake and maybe give a solution to this problem.
Thanks
David
function [x,y] = getpts_zoom(pic_analyze)
% open preloaded picture
image(pic_analyze)
axis equal
% create button
button=uicontrol('style','push','Position',[20 20 100 50],'String','Get Points','Callback',@(x,y)buttonpress_getpoints);
% subfunction getpts
function [x,y] = buttonpress_getpoints
[x,y] = getpts;
end
end

回答(1 个)

function [x,y] = getpts_zoom(pic_analyze)
x = []; % Must be specified in the main workspace.
y = []; % Must be specified in the main workspace.
% Show picture
image(pic_analyze)
axis equal
% Create button:
uicontrol('style','push','Position',[20 20 100 50],'String','Get Points','Callback',@mynest);
% Nested function:
function mynest(~,~)
[x,y] = getpts();
end
waitfor(gcf)
end
Read about nested functions:

类别

帮助中心File Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品

版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by