Advanced: Getting an output from a GUIDE GUI10

Posted by Doug Hull,

I had a well formed question from an advanced MATLAB user recently. He wanted to have an output come back from a GUI. There is an example of this with the modal dialog example in GUIDE. However, it takes some effort to know what parts of the example give this particular behavior. In this video, I make a very simple GUI that gives a return value by pasting from that example.
Here is the code I copied and pasted:

%%% OpeningFCN
uncomment uiwait

%%% OutputFCN
varargout{1} = handles.output;
% The figure can be deleted now
delete(handles.figure1);

%%% CloseReqFCN
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(hObject);
else
% The GUI is no longer waiting, just close it
delete(hObject);
end


Gary replied on : 1 of 10
You can also get data out from a GUI while the GUI is running. I have done this to batch-run a GUI on a bunch of datafiles and get results after each file. The batch code looks something like this:
hfig=GUIName;
[Handles OutputData]=GUIName('GUIName_OutputFcn',hfig,0,guidata(hfig));

Inside the GUI code, I have:
function varargout = GUIName_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
varargout{2} = OutputData;

Tannistha replied on : 2 of 10
Doug, Thank you so much for your tutorial.I was trying to call a subGUI as a function and had no clue where i was going wrong with my output.Finally,thanks to you i figured it out.
Dave replied on : 3 of 10
Very helpful! Of the many responses in MATLAB Central, none had the exact steps laid out with enough detail and as clearly as you have here.
dhull replied on : 4 of 10
Funny, watching this again after a year and a half, I was like "How did I figure that out?" I laughed when at the end of the video I admitted to just copying and pasting! I make these videos as reminders to me too, you can't remember it all!
David Goldsmith replied on : 5 of 10
Follow-up: I'm glad you indicated where you got this from, 'cause I found from there that I also needed to add guidata(hObject, handles); to my callbacks (I don't know if your callbacks automatically had this, e.g., due to some GUIDE preferences setting, but mine didn't). Thanks again!
Marco replied on : 6 of 10
I need to change the information of a parent handle. I have a main gui that displays an image, from one of the pull down menus the user can choose to perform an editing operation (such as global thresholding), once the user clicks on this menu the Main gui call another gui and passes its handles as a parameter. The sub gui allows the user choose specifics. How do I make sure that the changes in the subgui affect the changes in the Main gui. I have no problem displaying the new image, its just that it does not "save" in the main gui handes. I tried updating the main handles with guidata but that only updates it within the subgroup. I also tried outputing the new changed image however whenever I call uiwait(hObject) my subGui disappears. On task manager I can see it is there but I just cannot click on it (or even see it). I did make sure visibility was on, I changed the uistack so that it was on top. I tried changing it to a modal window. But none of these work, I followed the steps outlined in this video but again, uiresume just screws everything up. Any help would be appreciated!!
doug replied on : 7 of 10
@marco I think this video will help: https://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/
Hector replied on : 8 of 10
Hello I'm new with GUIDE und GUIs and This is exactly what I'm trying to do but I need a total of 7 checkboxes. When I try to add the second one I can only get the output of the first one. I have tried diferent ways but nothing seems to work. I appreciate any help. This is what I have in my output function, (the rest is exactly as in the example). function varargout = myGUI_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) varargout{1}=get(handles.checkbox1,'value'); varargout{2}=get(handles.checkbox2,'value'); %varargout=handles.output; % The figure can be deleted now delete(hObject);
doug replied on : 9 of 10
@Hector, It looks like you are creating several outputs. Ar you capturing them? [A,B] = max(rand(1,10)); You could concatenate all those values into one vector first and just pass one thing out also. doug
Bart McCoy replied on : 10 of 10
Thanks so much-- this was very helpful and very accurate! It works perfectly! I get it now. In normal GUI operation, 3 functions are called in quick succession, before the user even interacts with the GUI: 1) GUI constructor function (same name as GUI 2) The "OpeningFcn" and 3) The "OutputFcn", which quickly returns output args, perhaps before you are ready. If you want to prevent the GUI from immediately running the output function, you run UIWAIT inside #2 (OpeningFcn). Whenever a UIRESUME appears anywhere in the code, the "OutputFcn" is run. Finally, you don't want to delete(handles.figure1) until you have assembled all your return values in "OutputFcn"... makes sense now. Last thing to do at the end of the "OutputFnc" is to call delete(handles.figure1) to turn out the lights & shut the door! Now I can make my own small MATLAB GUI wrappers around functions and immediately return the results!! The only thing I didn't understand was why the CloseRequestFcn needs to have an if/then statement, running either UIRESUME or delete. If you KNOW your OpeningFcn is going to call UIWAIT and you KNOW you want to return things from OutputFcn, you'd never want to run "delete" inside the CloseRequestFcn or it destroys the whole window. Seems like a simpler method (which I am now using) would be: a) At the end of OpeningFcn, call "UIWAIT(handles.figure1)" b) In the CloseRequestFcn (or your favorite close-button callback), simply run "UIRESUME(handles.figure1).... no IF... ELSE. That will invoke a call to OutputFcn. c) Then in your OutputFcn, assemble all your return values and finally call delete(handles.figure1) at the end of OutputFcn to return values & clean up the window.

This site uses Akismet to reduce spam. Learn how your comment data is processed.