Adding a button which activates ginput to give values
Show older comments
Hi there, I have been having some issues trying to add buttons and calling functions and functions handles. This is what I had...
but=uicontrol('Style', 'pushbutton',...
'String', 'Pick Maximum Trace',...
'Position', [10 10 200 20]);
set(but,'Callback',{'pick',p});
Seperate file...
function [x,y]=pick(hObj,event,p) %#ok<INUSD
[x,y]=ginput(p);
end
The problem is trying to get out the x and y variables from the pick function. How do I get output variables from a callback?
Cheers for the help, James
Answers (2)
Jan
on 4 Jul 2011
Where do you want to get these outputs?
You can store them e.g. in the GUI's UserData or by GUIDATA:
function pick(hObj, event, p)
[x,y]=ginput(p);
FigH = ancestor(hObj, 'figure');
set(FigH, 'UserData', [x, y]);
% Or:
setappdata(FigH, 'LastPoint', [x, y]);
% Or:
handles = guidata(FigH);
handles.InputPoint = [x, y];
guidata(FigH, handles);
end
The methods to get these values later are similar: get(FigH, 'UserData'), or getappdata, or the GUIDATA approach.
Matt Fig
on 4 Jul 2011
One approach would be to make the callback for your button a function in the GUI file which immediately calls the PICK function.
function [] = mybuttoncallback(varargin)
% Callback for button.
[x,y] = pick(...)
% Now save in GUIDATA or whatever...
Then you have your x and y in this workspace to save to the appdata with GUIDATA or whatever. This gives you more control over the callback, as you can use try-catch in searching for the PICK file.
The other alternative is to forget about the PICK function altogether and just put the call to GINPUT directly into the callback which is in the GUI file.
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!