MATLAB default figure font sizes

14,233

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.

Share:
14,233
Hanmyo
Author by

Hanmyo

I am a mathematics and computer science undergraduate student, soon to be an applied mathematics graduate student! I like pillows and snowcones a lot, too.

Updated on June 04, 2022

Comments

  • Hanmyo
    Hanmyo almost 2 years

    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?