Setting default values of GUI controls using the CreateFcn
Show older comments
Hi all,
I'm having an issue building a programmatic GUI for a small app I'm currently working on. The GUI has a number of sliders and edit boxes to modify the parameters of an aircraft which can then be further analysed. I'm trying to set the default values of those sliders and edit boxes with data from a .mat configuration file I have created separately.
I know that it's possible to set the default values of these graphics objects when you write the code to create them (ie uicontrol('Style', 'slider', 'Value', 50)), but the numbers will require some processing and given I have 40+ controls in total, it's a little impractical to do it all manually. What I'd like to do (ideally) is have a function that executes when the GUI is created that sets all the default values of all the controls then. I already have a way of updating all the parameters in an easy and scalable way but am struggling to find somewhere to execute the code. My method relies on getting the list of uicontrol objects using 'findobj' and specifying edit and slider objects, then looping through that list of returned objects and matching the tags to variable names in the configuration file. I've tested this and it seems to work well.
I thought that adding this code to the 'CreateFcn' of the GUI figure would work perfectly, as the description says this function is executed 'just before the figure is made visible' but when I run my code in this opening function 'findobj' doesn't return a list of values, just a 0x0 array. I'm guessing this is because the createFcn is executing earlier than I expected when the uicontrols haven't actually been created.
Does anyone have any ideas on how I could make this work?
Many thanks for any help,
Chris
2 Comments
How are you creating the sliders and editboxes with their tags in the first place? It would be much better to keep an array of handles to these when you create them than rely on a load of findobj calls at a later stage.
Also where are you creating the figure? I usually do my programmatic GUIs in a class so my figure is created there and I store handles to all the components I create in the class object for later use. If I have many sliders I may store these in an array, though I usually use individually named properties. If I have functionality that requires them all I create a dependent property that puts all the slider handles into an array.
If you are creating the figure in a function can't you just call your initialisation function at the bottom of whatever function creates the figure?
Christopher Dadswell
on 3 Jun 2015
Accepted Answer
More Answers (1)
do you mean you have pasted it in the OpeningFcn?
I have not used the findobj there but I have used it to set background images of pusbuttons so it seems that the uicontrols are already present
worst case scenario you could also just add a menu bar like File-Load and than you can even specify from which mat.file you would like to read in the settings. This option could also be handy if you want to restore to default values after you have made some changes without the need to close and open the app again
2 Comments
Christopher Dadswell
on 2 Jun 2015
Ingrid
on 2 Jun 2015
I was indeed assuming that you were using GUIDE because when you are creating the GUI programatically there is no need to use the findobj but it is better to store the handles of the uicontrols directly as also suggested by Adam
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!