I've found that I can put set(0, 'DefaultAxesFontSize',14)
in a startup.m file, which then changes the default font size of ticks, axes labels, and title of my figures. Is it possible to have a separate default font size for the title or axes labels?
'14px' (default) | positive integer | string scalar | character vector. Live Editor normal font size, specified as one of the following: A positive integer representing the size of the font in points, in the range [0,100] .
When I produce a text object in a MATLAB figure, these fonts are listed as fonts in the property editor for the text object, but the default Helvetica font is used by MATLAB if they are chosen.
To change the font size, set the “FontSize” property for the axes. Since many plotting functions reset axes properties, including the font size, set the "FontSize" property after plotting. For example, the code below sets the font size to 16 points. The tick labels use the specified font size.
As explained in the documentation, to change the default figure units and position, set the respective properties like this: set(0, 'defaultFigureUnits', 'centimeters', 'defaultFigurePosition', [0 0 8.86 7.8]);
You cannot have a separate default font size for titles and labels with the standard mechanisms. If you are willing to overload the labelling commands, then you can come pretty close. The easiest would be to modify xlabel
to allow for a default font. You would need to add
if ~isempty(getappdata(ax, 'DefaultAxesXLabelFontSize'))
set(h, 'FontSize', getappdata(ax, 'DefaultAxesXLabelFontSize'));
else
if ~isempty(getappdata(get(ax, 'parent'), 'DefaultAxesXLabelFontSize'))
set(h, 'FontSize', getappdata(get(ax, 'parent'), 'DefaultAxesXLabelFontSize'));
elseif ~isempty(getappdata(0, 'DefaultAxesXLabelFontSize'))
set(h, 'FontSize', getappdata(0, 'DefaultAxesXLabelFontSize'));
end
end
immediately before
set(h, 'String', string, pvpairs{:});
If you don't want to modify a core file you can overload xlabel
function varargout = xlabel(varargin)
ax = axescheck(varargin{:});
if isempty(ax)
ax = gca;
end
oldPath = pwd;
cd([matlabroot, filesep, 'toolbox', filesep, 'matlab', filesep, 'graph2d']);
xlabel = str2func('xlabel');
cd(oldPath);
oldFontsize = get(ax, 'FontSize');
if ~isempty(getappdata(ax, 'DefaultAxesXLabelFontSize'))
set(ax, 'FontSize', getappdata(ax, 'DefaultAxesXLabelFontSize'));
else
if ~isempty(getappdata(get(ax, 'parent'), 'DefaultAxesXLabelFontSize'))
set(ax, 'FontSize', getappdata(get(ax, 'parent'), 'DefaultAxesXLabelFontSize'));
elseif ~isempty(getappdata(0, 'DefaultAxesXLabelFontSize'))
set(ax, 'FontSize', getappdata(0, 'DefaultAxesXLabelFontSize'));
end
end
varargout{1:nargout} = xlabel(varargin{:});
set(ax, 'FontSize', oldFontsize);
if ~nargout
varargout = {};
end
end
Either way, you can set the default font size with
setappdata(0, 'DefaultAxesXLabelFontSize', 36)
or
setappdata(gcf, 'DefaultAxesXLabelFontSize', 36)
or
setappdata(gca, 'DefaultAxesXLabelFontSize', 36)
Note that it uses setappdata
and not set
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With