Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Output from GUI's

Subject: Output from GUI's

From: Jane

Date: 18 Jul, 2007 13:19:15

Message: 1 of 6

Hi,

I need help...

I'm running a script file that will open a GUI to get some information from the user. What I need to know is how do I then output variables from the GUI for use further down the script file?

I've only ever been able to do this by writing the required variables to a .mat file, which I then load in the script file. Is there a more elegant method?

Thanks
Jane

Subject: Output from GUI's

From: David

Date: 18 Jul, 2007 13:33:22

Message: 2 of 6

well, a slightly more elegant solution is to use evalin or assignin to put the values in the base workspace.

the 'proper' method is to assign the outputs to the handles structure that gets returned as the varargout values.

Subject: Output from GUI's

From: Jane

Date: 18 Jul, 2007 13:43:24

Message: 3 of 6

I don't get my handles structure as an output, although I have tried.

Is there anywhere that describes this in a clear 'for Dummies' type way? I've found the Mathworks literature on the subject very confusing.

Subject: Output from GUI's

From: David

Date: 18 Jul, 2007 13:52:36

Message: 4 of 6

yeah, its not exactly obvious, and there are several steps.

i 'think' you have to use uiwait in the _OpeningFcn to get the caller to wait for the return values.

first, you have to set the value somewhere like this:
    handles.output='Error';
    guidata(hObject,handles);
    
then in the _OutputFcn assign it to the output like:
    varargout{1} = handles.output;
then delete the figure (or do uiresume?)

I have gotten it to work once and haven't had to play with it again... i recently had to make another small gui and resorted to assignin to base workspace instead of sorting it out again.

Subject: Output from GUI's

From: Gautam Vallabha

Date: 18 Jul, 2007 12:58:30

Message: 5 of 6

"Jane " <j.l.terry@hud.ac.uk> wrote in message
news:f7l40j$8ln$1@fred.mathworks.com...
> I'm running a script file that will open a GUI to get some information
> from the user. What I need to know is how do I then output variables from
> the GUI for use further down the script file?
> I've only ever been able to do this by writing the required variables to a
> .mat file, which I then load in the script file. Is there a more elegant
> method?

As David mentioned, you could use EVALIN.

Another way is to modify the outputs of the GUI (I assume you are using a
"modal question" GUI). If your gui name is "mygui", then:

1) In mygui.m, find mygui_OutputFcn.

2) Gather the data you want to output.
s.name = get(handles.edit1, 'String');
s.age = str2num(get(handles.edit2, 'String'));
s.genderID = get(handles.listbox1, 'value');

3) Assign the structure to varargout
varargout{1} = s;

Now, invoke your GUI like this:
>> out = mygui
and you will get your structure s as the output.

--

Gautam Vallabha
The MathWorks
Email: Gautam.Vallabha@mathworks.com

Subject: Output from GUI's

From: Matteo

Date: 3 Apr, 2008 09:47:01

Message: 6 of 6

"Gautam Vallabha" <gvallabh@mathworks.com> wrote in message
<f7lgrm$114$1@fred.mathworks.com>...
> Another way is to modify the outputs of the GUI (I assume
you are using a
> "modal question" GUI). If your gui name is "mygui", then:
>
> 1) In mygui.m, find mygui_OutputFcn.
>
> 2) Gather the data you want to output.
> s.name = get(handles.edit1, 'String');
> s.age = str2num(get(handles.edit2, 'String'));
> s.genderID = get(handles.listbox1, 'value');
>
> 3) Assign the structure to varargout
> varargout{1} = s;
>
> Now, invoke your GUI like this:
> >> out = mygui
> and you will get your structure s as the output.
>
> --
>
> Gautam Vallabha
> The MathWorks
> Email: Gautam.Vallabha@mathworks.com
>
>

I have a very similar problem I'm looking for a
solution as well. In my case, I understood that I have to
use uiwait in the _OpeningFcn function to hold MATLAB
command line and wait fot the user to set values on the GUI.
My code is like:

% Code start
function xxx_OpeningFcn(hObject, eventdata, handles,
varargin)
uiwait(handles.figure1);

function figure1_CloseRequestFcn(hObject, eventdata,
handles)
uiresume();
delete(hObject);

function varargout = xxx_OutputFcn(hObject, eventdata,
handles)
varargout{1} = handles.output;

<somewhere in the code>
handles.output=data;
guidata(handles.figure1,handles);

% Code end

This does not work, though. Apparently, when the _OutputFcn
is invoked after having closed the window, the "handles"
have already been destroyed. In fact, it results to me that
the variable "handles" in _OutputFcn is empty.
The _CloseRequestFcn is actually executed before the
_OutputFcn, but for some reason uiresume does not do the
job, and when _OutputFcn is called, the figure has already
been killed and all the handles and data lost.

Can someone help, please? Many thanks.

Matteo

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us