Setting proprties for multiple UIControl objects at once (or setting a default)

5 views (last 30 days)
Hey, I'm creating a complex GUI that will have many similar objects (for example, text). I want all of them to have the same properties, besides the text, for example:
scanParametersFromLabel = uicontrol('Parent', scanParameters, ...
'Style', 'text', 'String', 'From', 'FontSize', fontSize, 'ForegroundColor', [0.5 0.5 0.5]);
scanParametersToLabel = uicontrol('Parent', scanParameters, ...
'Style', 'text', 'String', 'To', 'FontSize', fontSize, 'ForegroundColor', [0.5 0.5 0.5]);
scanParametersNPointsLabel = uicontrol('Parent', scanParameters, ...
'Style', 'text', 'String', '# Points', 'FontSize', fontSize, 'ForegroundColor', [0.5 0.5 0.5]);
scanParametersFixLabel = uicontrol('Parent', scanParameters, ...
'Style', 'text', 'String', 'Fix', 'FontSize', fontSize, 'ForegroundColor', [0.5 0.5 0.5]);
scanParametersPositionLabel = uicontrol('Parent', scanParameters, ...
'Style', 'text', 'String', 'Position', 'FontSize', fontSize, 'ForegroundColor', [0.5 0.5 0.5]);
Is there anyway to quickly set all of the parameters for all objects? or maybe set a default value? It'll remove a lot of clutter from my code... For example something like this would be great:
textProprties = sprintf('''Style'', ''text'', ''String'', ''From'', ''FontSize'', fontSize, ''ForegroundColor'', [0.5 0.5 0.5]');
scanParametersFromLabel = uicontrol('Parent', scanParameters, 'String', 'From', textProprties);
I know I can do it with eval(sprintf(...)), but I was hoping for something more elegant...

Accepted Answer

Jan
Jan on 11 Feb 2017
Edited: Jan on 11 Feb 2017
scanParameters = figure;
fontSize = 12,
props = {'Parent', scanParameters, ...
'Style', 'text', 'FontSize', fontSize, 'ForegroundColor', [0.5 0.5 0.5]};
uicontrol('String', 'From', props{:});
I'd prefer this for clarity:
scanParameters = figure;
Props.Parent = scanParameters;
Props.FontSize = 12;
Props.Style = 'text';
Props.ForegroundColor = [0.5 0.5 0.5];
uicontrol('String', 'From', Props);
  1 Comment
Yoav Romach
Yoav Romach on 11 Feb 2017
Amazing, all I needed to do was to put it in a Cell... I even thought about doing it, didn't know why I didn't try :\
Thanks a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!