Effacer les filtres
Effacer les filtres

How can i execute a GUI from a .m script?

56 vues (au cours des 30 derniers jours)
Brian Dennehy
Brian Dennehy le 28 Nov 2014
Commenté : giacomo le 23 Nov 2017
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

Réponse acceptée

Geoff Hayes
Geoff Hayes le 28 Nov 2014
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)?
  1 commentaire
giacomo
giacomo le 23 Nov 2017
How can I change the very first line:
hFig = figure;
if I want the remainder code to run after I close my GUI called mygui? I tried the easy:
hFig = mygui;
But then the mygui interface keeps flashing on my screen and I can't even use it. Thanks.

Connectez-vous pour commenter.

Plus de réponses (2)

Brian Dennehy
Brian Dennehy le 4 Déc 2014
Thank you very much!!! I really appreciate this answer and found it very useful. My mistake was a silly mistake. I thought there was a command to launch myGui, no that you had to just put myGui; as a command. Now it works perfectly. Once again, thank you very much!
I know what you mean about waitfor(), i'm learning how does that work. I need to launch the GUI within a for loop in my script. I need the user to choose a special transformation for each rectangle within a given grid, so i need the script to wait for each option he chooses after moving to the next cycle of the loop.

msand65
msand65 le 9 Juin 2017
Modifié(e) : msand65 le 9 Juin 2017
Thank you, we launch Matlab using activeX and the 'tag' and waitfor solution from Geoff Hayes works for launching a GUI from the script that is called. (No need to pass data back to the GUI in this case.)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by