MATLAB: Setting default values of GUI controls using the CreateFcn

createfcndefault slider valuesgui

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

Best Answer

Something like this would work to store the handles. I tend to prefer a more explicit link between a slider and its associated edit box, but given the way they are created, if nothing else changes them you know that the nth slider in the hSliders array corresponds to the nth edit box in the hEditBoxes array.
hSliders = [];
hEditBoxes = [];
for i = 1:length(strings)
uicontrol('Parent', acpg_tab2, 'Style', 'text',...
'String', strings{i},...
'FontSize', 8, 'HorizontalAlignment', 'left',...
'Units', 'normalized',...
'Position', [0.05 (0.995 - 0.05*i) 0.3 0.03],...
'HandleVisibility', 'callback', 'Tag', labeltags{i});
hSliders(i) = uicontrol('Parent', acpg_tab2, 'Style', 'slider',...
'Min', 0, 'Max', 100, 'Value', 25,...
'Units', 'normalized',...
'Position', [0.375 (1-0.05*i) 0.4 0.03],...
'HandleVisibility', 'callback', 'Tag', slidertags{i},...
'Callback', @sliderChanged);
hEditBoxes(i) = uicontrol('Parent', acpg_tab2, 'Style', 'edit',...
'Fontsize', 8, 'HorizontalAlignment', 'left',...
'Units', 'normalized',...
'Position', [0.80 (1-0.05*i) 0.15 0.03],...
'HandleVisibility', 'callback', 'Tag', edittags{i},...
'Callback', @editChanged);
end
If you still prefer to use tags to then do the initialisation then you can do that with this method (though using the indices would be simpler). You just have to do some string manipulation e.g.
sliderTags = get( hSliders, 'Tag' );
sliderControl = hSliders( find( strcmp( sliderTags, requiredTag ) ) );
to get the slider control with 'requiredTag'.
An initialisation method using this should be able to go at the end of your creation function because it does not rely on findobj so it doesn't need the figure to be visualised before you can do your initialisation.
Related Question