I'm using Matlab guide to create user interfaces. Everything works fine bit the grid seems to change depending on the machine. The grid is set to to 10 pixels and snap to grid is on. All units are set to characters. Why is the grid changing and controls aren't on the grid anymore?
1 Answers
Per the documentation, MATLAB defines the 'characters' units as follows:
These units are based on the default
uicontrolfont of the graphics root object:
Character width = width of the letter
x.Character height = distance between the baselines of two lines of text.
To access the default uicontrol font, use
get(groot,'defaultuicontrolFontName')orset(groot,'defaultuicontrolFontName').
This default font can vary between systems and should not be used to lay out your GUI (Why MATLAB used it as the default for so long I don't know). I'd recommend using 'pixels' or 'normalized' units for controlling your GUI's layout.
To illustrate the difference, try out the following example:
h1.f = figure('Name', get(groot, 'defaultuicontrolFontName'), ...
'NumberTitle', 'off', 'ToolBar', 'none' ...
);
h1.b1 = uicontrol('Parent', h1.f, 'Style', 'pushbutton', ...
'Units', 'characters', 'Position', [35 15 45 10 ], ...
'String', 'Button A' ...
);
h1.b2 = uicontrol('Parent', h1.f, 'Style', 'pushbutton', ...
'Units', 'characters', 'Position', [35 5 45 10 ], ...
'String', 'Button B' ...
);
oldfont = get(groot, 'defaultuicontrolFontName');
set(groot, 'defaultuicontrolFontName', 'Comic Sans MS');
h2.f = figure('Name', get(groot, 'defaultuicontrolFontName'), ...
'NumberTitle', 'off', 'ToolBar', 'none' ...
);
h2.b1 = uicontrol('Parent', h2.f, 'Style', 'pushbutton', ...
'Units', 'characters', 'Position', [35 15 45 10 ], ...
'String', 'Button A' ...
);
h2.b2 = uicontrol('Parent', h2.f, 'Style', 'pushbutton', ...
'Units', 'characters', 'Position', [35 5 45 10 ], ...
'String', 'Button B' ...
);
set(groot, 'defaultuicontrolFontName', oldfont);
Which produces 2 figure windows:
Note that both figures are generated with the exact same code, yet their layouts are drastically different.


Characterunit varies based on the default system font. I'd highly recommend not using it to arrange your GUI elements. - excaza