Wednesday, December 04, 2013

Getting output from a Matlab GUI

Let's say you have a GUI like the one on the right as part of a program that runs a mouse in a simple behavioral task. The GUI requires the user to manually enter some data (the animal's name) and verify that the power is on in your setup. We want the GUI to close and return the relevant outputs when the 'Start Program' button is pressed by the user.

There are three tweaks you will need to make this work, if you made your GUI using the GUIDE functionality in Matlab (note if you want to tinker with an example, I've included one at the end of this post).

1) Make the GUI wait before it returns outputs
The GUI will try to return outputs right when it is invoked, well before any of the values can be specified by the user. To block such behavior, you can pause program execution using the uiwait command at the end of OpeningFcn:
   uiwait(hObject);  
This will make the GUI wait until some additional action is performed (e.g., uiresume is called to resume program flow, or the GUI (with handle hObject) is closed). This gives the user time to enter the actual values. Note you should put this command at the end of OpeningFcn.

2) Specify the output variables
Within OutputFcn, use varargout to specify what outputs you want returned from the GUI. Something like:
 varargout{1} = handles.data1; 
 varargout{2} = handles.data2;
Matlab typically passes information among the different elements of a GUI using the handles variable, so this code just exploits this fact. Once the outputs are specified as above, they will be returned as outputs to the calling function for the main GUI:
 [data1, data2]=GUI_Practice; 
Where GUI_Practice is the name of the m-file that defines the GUI.

Caveat: you will probably define the desired outputs (such as handles.data1) within a callback function (using something like handles.data1=x). When you do so, be sure to enter the following within the callback function:
guidata(hObject, handles); 
This saves the local variable handles to the GUI handle, so they will not be annihilated outside the scope of the callback function.

3) Tell the program when to resume
If you only did the above steps, after calling uiwait the program would hang indefinitely. You need to call the uiresume command to bump the program out of wait mode. To resume program flow when the user clicks Start Program, add uiresume to CloseRequestFcn:
%When user clicks button, check to see if GUI is in wait  %mode. If it is, resume program; otherwise close GUI
 if isequal(get(hObject, 'waitstatus'), 'waiting')
     uiresume(hObject);
 else
     delete(hObject);
 end
Note: you should also add the  line delete(hObject); to the end of outputFcn. Otherwise, the user will have to attempt to close the GUI twice: once to resume program flow with uiresume, and again to close the GUI with delete.
   

Example
Below is a simple example called GUI_Practice (m-file and fig file are both needed for this to run, as it was made with GUIDE). Once the files are in your Matlab path, you can instantiate the GUI by entering animal_name=GUI_Practice;
GUI_Practice.m
GUI_Practice.fig

Acknowledgment
I got some of the ideas for this from Mathworks (here). If you are having trouble getting it to work, let me know in the comments, and I'll try to help.

 

2 comments:

Anonymous said...

Thanks for this tip. It was very helpful.

I also wanted to point out that it matters where you put the uiwait command. You should put it at the very end of the OpeningFcn function, or the output for the GUI might not update properly (and will output the initialization value of your variable).

I learned this through an hour of frustration today.

Eric Thomson said...

Anon I have added that detail to the post: thanks for the tip.