Programatically add OpeningFcn in GUI

17 Ansichten (letzte 30 Tage)
Haider Ali
Haider Ali am 22 Okt. 2018
Kommentiert: Walter Roberson am 26 Okt. 2018
Hi,
I have programatically created a GUI app using a single function file (e.g. mygui.m) and putting all my code in that single file. Now I need to add an OpeningFcn in that code. Where and how should I add that OpeningFcn in my code so that I can add data in handles variable in that OpeningFcn to access it later in my other functions?
Thanks
  4 Kommentare
Kevin Chng
Kevin Chng am 24 Okt. 2018
Bearbeitet: Kevin Chng am 24 Okt. 2018
Using handles to pass data is what we favour to do in GUIDE Gui.
I guess it should be also workable in programmically GUI. There is some knowledge require to structure the object and handle in your GUI like what we will see in GUIDE Gui (Guide generate it automatically).
Hence, I will recommend you use global to pass your data.
Walter Roberson
Walter Roberson am 24 Okt. 2018
Don't use global!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Okt. 2018
OpenFcn is not part of MATLAB itself. It is part of GUIDE. The boilerplate code inserted by GUIDE calls a routine that sets up OpenFcn to be called.
The handles structure itself is also an invention of GUIDE, but guidata() is part of MATLAB itself.
What is part of MATLAB itself is CreateFcn callbacks for most objects. The order that the object callbacks are invoked is not made clear.
You can add a CreateFcn callback to your figure. However at the time it is invoked your handles structure is not likely to be fully populated yet, partly because the handles structure is a GUIDE artifact.
The way that the handles structure is created in GUIDE is that the all of the objects are created first (by loading the stored figure) and after that, after all of the CreateFcn have been run, GUIDE finds all of the tagged handles that are children of the figure and creates a struct from them and only then guidata() it into place as the handles structure.
The good news is that since you are creating your figure elements by code, instead of adding a OpenFcn callback, you just put whatever code you want after you create the objects. Possibly including creating a handles structure if you happen to like that programming model.
Note that the presence of the handles structure as an argument to gui callbacks is yet another GUIDE artifact. MATLAB does not pass handles around by itself: GUIDE manipulates the various Callback and related *Fcn properties to add code that passes in the handles structure.
  3 Kommentare
Haider Ali
Haider Ali am 26 Okt. 2018
@Walter Roberson, thanks for the suggestion.
I ended up creating initializing all my ui components in the CreateFcn of my GUI figure and then populating the handles figure in that function. That solved my problem/
Walter Roberson
Walter Roberson am 26 Okt. 2018
Typically if you are programming the GUI yourself, you would have a figure() call in your code, and you would code all of the uicontrol() and whatever was appropriate. Typically you would not code a CreateFcn callback for a single figure unless the figure was to be saved and loaded afterwards, in which case the graphics objects would be saved with it but CreateFcn would be used to initialize various state variables, or perhaps to populate a listbox with contents according to the current directory.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by