MATLAB: How can i execute a GUI from a .m script

matlab gui script execute

Hello everybody!
I have created a .m script that makes some calculations. The thing is that in the middle of the script, i need the user to tell me which transformation should he choose (it is just one step from the whole script), for every rectangle in a given grill. So i created a GUI wich contains 5 buttons (the transformations options).
My problem is that i don't certainly know how to execute the GUI in my script…. is there a function or a command for that? How can i access it from the .m?
Thanks for your attention,
BRD

Best Answer

Brian - have you created your GUI through GUIDE or built it yourself using a script? Either way, you will have an m file which you can call directly from your current script as
% my other code

% launch GUI

myGUI;
% continue with script

where myGui.m is the name of you GUI m file. The trick with the above is how to wait, in the script, for your GUI to finish because you don't want the remainder of your script to be executed while the user is choosing a transformation.
Suppose that your GUI name/tag is tformChoiceGui and its HandleVisibility property is on, both which have been set using the GUIDE property editor for your figure/GUI. Or, if manually creating your GUI, you have done something like
hFig = figure;
set(hFig,'Tag', 'tformChoiceGui','HandleVisibility','on');
% continue to build GUI
You can then use the findobj to get the handle to your GUI, and wait for it to be closed/deleted using waitfor which will block execution until the GUI has been closed. Your code then becomes
% my other code
% launch GUI
myGUI;
% get the handle to the GUI and wait for it to be closed
hGui = findobj('Tag','tformChoiceGui');
waitfor(hGui);
% continue with script
See the attached (very simple) example of a script, myScript, that launches myGui and waits for it to close. One thing that is obviously missing is how to get the transformation choice from your GUI back to the script. How are you planning on doing this (without using global variables)?