One method for doing this is to use the WAITFOR function and have the code wait for a particular property of a particular graphics handle to change.
In the following example, the GUI has a popup menu and a 'Close' button. The GUI is called by passing in a number.
This number is used to set the value of the popup menu. The user then clicks on the 'Close' button to close the GUI. At this time, the value selected by the user is passed out into 'a'.
In this example, the GUI function contains the following:
if nargin == 1
fig = openfig(mfilename,'reuse');
set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));
handles = guihandles(fig);
guidata(fig, handles);
set(handles.popupmenu1,'Value',varargin{1})
set(handles.pushbutton1,'UserData','edit')
waitfor(handles.pushbutton1,'UserData','read')
if nargout > 0
varargout{1} = get(handles.popupmenu1, 'Value');
delete(fig);
end
elseif ischar(varargin{1})
try
[varargout{1:nargout}] = feval(varargin{:});
catch
disp(lasterr);
end
end
The callback for the popupmenu is not used. However, the callback for the pushbutton sets the 'UserData' property value to 'read'.
Once this occurs, the WAITFOR function resumes execution of the code and the value of the popupmenu is retrieved. The value is passed to VARARGOUT and the GUI is closed.