Pass GUI information to another GUI depending on a condition

1 view (last 30 days)
I have a GUI (built in GUIDE), lets call it GUI 1, that opens another GUI, lets call it GUI 2, when the users selects a check box. GUI 2 asks the user to select three different file locations using press buttons. Then, the user is supposed to hit a button called "Submit File Names" when they are done selecting the three file locations. The "Submit File Names Button" is supposed to send the file names back to GUI 1 and to prompt GUI 1 to complete its next series of tasks. I cannot figure out how to get GUI 2 to send the file locations back into GUI 1 without adding an additional button in GUI 1.
Here is the check box code from GUI 1:
%Check box code from GUI 1:
function checkBox_Callback(hObject,eventdata,handles)
% hObject handle to checkBox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data
button_state = get(hObject,'Value');
if button_state == 1
GUI2();
% This is where I get stuck. How do I pause this section of code while
% the user selects their files in GUI2 and then pick up here when they
% press the "Submit File Names" button? This is what a pseudo code
% would look like:
%Pause GUI1 here until GUI2 is finished
%Recieve three variables from GUI2
%Restart GUI1 code
else
%Not important to the question.
end
Here is the push button code for "Submit File Names"
%Submit File Name code from GUI2:
% --- Executes on button press in submit
function submit_Callback(hObject,eventdata,handles)
% hObject handle to submit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data
% This is the other place I get stuck. How do I get this button press to
% continue with the GUI1 code up above after it sends the three variable
% back to GUI1? This is what a pseudo code would look like:
%Send three variables to GUI 1 at the point where it was paused
end

Accepted Answer

Josh
Josh on 10 May 2019
I don't usually use GUIDE, so forgive me if I explain something that GUIDE takes care of automatically.
You'll probably want the GUI2 to be shown in a modal window. This will prevent the user from accessing GUI1 while GUID2 is running. Basically it treats GUI2 as a dialog-box:
% Put this in your GUIDE OpeningFcn
set(hObject, 'WindowStyle', 'modal');
To freeze execution until GUI2 is closed (or until you tell execution to continue), put this line at the end of the GUI2's OpeningFcn:
% Wait until uiresume is called or figure is closed
uiwait(hObject);
At the end of submit_Callback, you'll want to call uiresume to start executing your code again:
% Let GUIDE resmue execution
uiresume(ancestor(hObject, 'figure'));
Before GUIDE finishes, running your code, it calls OutputFcn to set the your GUI's outputs. You'll want to assign you're output file names here.
% Assign file names to GUIDE output (in OutputFcn)
% (I'm not sure how your file names are stored so I put in a plcae holder)
varargout{1} = file_names;
Be sure to delete GUI2's figure at the end of it's OutputFcn (this will allow your other window to receive focus again):
delete(hObject);
In GUI1, you'll want to have a call like:
file_names = GUI2();
This will start up the GUIDE function for GUI2. On startup, GUIDE will call GUI2's OpengingFcn, which will pause execution until you either close GUI2's window or until uiresume is called in the submit_Callback.
Once execution is resumed, GUIDE will call OutputFcn to set the output values for GUI2. These will then get stored in file_names when control returns to GUI1.
Hope that helps.
  2 Comments
Charles D'Onofrio
Charles D'Onofrio on 10 May 2019
Josh,
Thanks for your help. Your suggestion worked very well. I had to play around with where to put uiwait(hObject) in the GUI 2 opening function and finally figured out that it needs to go at the end of all the other initializations I set up.
Even though your suggestions got the GUI to work, I still cannot say I quite understand how the uiwait(hObject) and uiresume(ancestor(hObject,'figure')) actually know which GUI to operate on. In this example, although they are called in GUI 2, they impact GUI 1. How did GUI 1 know these commands were telling it to wait and resume? Maybe my understanding of hObject is flawed?
I read the Mathworks documentation on uiwait, uiresume, and ancestor, but these did not help. Could you explain them in the context of this example a little better?
Josh
Josh on 11 May 2019
uiwait simply pauses program execution until the figure handle you passed to it (in this case hObject) is destroyed, or uiresume is called.
Remember, you're calling GUI2 as a function from within GUI1. As with any function call, GUI1 has to wait until the function call returns to procede executing its code.
As far as I can tell, this is what happens when you call a GUIDE-created function like GUI2:
  1. Call GUI2 (from GUI1 in your case)
  2. GUI2 executes code to create and setup its figure
  3. GUI2 calls OpeningFcn
  4. GUI2 calls OutputFcn
  5. GUI2 returns the outputs generated by OutputFcn to GUI1
I think you may be missing the fact that GUI1 always waits for step 5 before proceding, regardless of whether uiwait/uiresume are there. It normally happens so fast (step 2 happens instantaneously; it doesn't wait for you to perform any actions on the figure before proceding) that you might not have realized this was the case.
Adding uiwait/uiresume as I described freezes execution at the end of step 3 until GUI2's figure is disposed of.
If you're still confused, I'd put a breakpoint at your GUI2 call and step through the GUIDE initialization (with and without uiwait) so you can see clearly what's happening.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!