Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB default figure font sizes

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?

like image 427
Hanmyo Avatar asked Apr 20 '12 07:04

Hanmyo


People also ask

What is the default font size in MATLAB?

'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] .

What font do MATLAB figures use?

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.

How do I change font size in MATLAB figure?

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.

How do I change the default figure size in MATLAB?

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]);


1 Answers

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.

like image 135
StrongBad Avatar answered Oct 01 '22 05:10

StrongBad