Programatically add OpeningFcn in GUI

16 次查看(过去 30 天)
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 个评论
Kevin Chng
Kevin Chng 2018-10-24
编辑:Kevin Chng 2018-10-24
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 2018-10-24
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 个评论
Haider Ali
Haider Ali 2018-10-26
@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 2018-10-26
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by