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

답변 (2개)

Jan
Jan 2011년 7월 4일
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

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

댓글을 달려면 로그인하십시오.

Matt Fig
Matt Fig 2011년 7월 4일
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.

카테고리

도움말 센터File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

질문:

2011년 7월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by