Pass GUI information to another GUI depending on a condition
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Charles D'Onofrio
el 9 de Mayo de 2019
Comentada: Josh
el 11 de Mayo de 2019
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
0 comentarios
Respuesta aceptada
Josh
el 10 de Mayo de 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 comentarios
Josh
el 11 de Mayo de 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:
- Call GUI2 (from GUI1 in your case)
- GUI2 executes code to create and setup its figure
- GUI2 calls OpeningFcn
- GUI2 calls OutputFcn
- 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Debugging and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!