6
votes

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?

1
Relevant note: I checked out get(0,'Factory') and get(0,'default') and didn't see anything related to figure font size other than the one mentioned above... - Hanmyo
Last time I checked (Matlab2010b) there was no default for fontsizes besides DefaultAxesFontSize and DefaultTextFontSize. And I don't think they added it. - bdecaf

1 Answers

6
votes

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.