Adding a button which activates ginput to give values

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)

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.

1 Comment

All I'm trying to do is call the ginput, pick function and get the values of x and y out to use else where in the script like a normal function or sub-routine
so ideally something like
[x,y]=set(but,'Callback',{'pick',p});
but that doesn't work...I'll try your ideas Cheers

Sign in to comment.

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

Tags

Asked:

on 4 Jul 2011

Community Treasure Hunt

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

Start Hunting!