Main Content

fontname

Change font name for objects in a figure

Since R2022a

    Description

    fontname(fname) sets the font name of all the text within the current figure to fname. If the figure contains other graphics objects, such as UI components or an axes object with a legend, fontname also sets the font name for those objects in the figure. Use listfonts for a list of available system fonts.

    Before R2023a: All syntaxes require a graphics object as the first input, such as fontname(obj,fname).

    example

    fontname("default") resets the font name to the default, automatic font name.

    example

    fontname(obj,___) sets the font name of all the text within graphics object obj. If obj contains other graphics objects, fontname also sets the font name for those objects. Specify obj as the first input argument in any of the previous syntaxes.

    example

    Examples

    collapse all

    Create a plot containing two lines with a title and a legend.

    plot([0 1; 1 2])
    title("Two Very Straight Lines")
    legend("Blue Line","Red Line")

    Figure contains an axes object. The axes object with title Two Very Straight Lines contains 2 objects of type line. These objects represent Blue Line, Red Line.

    Change the font to Courier.

    fontname("courier")

    Figure contains an axes object. The axes object with title Two Very Straight Lines contains 2 objects of type line. These objects represent Blue Line, Red Line.

    Before R2023a: Specify gcf as the first argument to the fontname function. For example, fontname(gcf,"courier").

    Create the following function file, and save it as plotapplayout.m on your MATLAB® path. This function returns the layout for a simple app to plot data using different plot types.

    function fig = plotapplayout
    % Create figure window
    fig = uifigure;
    
    % Create UI components
    ax = uiaxes(fig,Position=[15 70 535 340]);
    lbl = uilabel(fig,Position=[30 15 110 35],Text="Choose Plot Type:");
    b1 = uibutton(fig,Position=[150 15 180 35],Text="Surf");
    b2 = uibutton(fig,Position=[350 15 180 35],Text="Mesh");
    
    % Configure UI component appearance
    surf(ax,peaks);
    title(ax,"Peak Surface")
    fontname(fig,"Lucida Bright")
    end
    

    Call the function and assign the returned figure object to f.

    f = plotapplayout;

    Figure contains an axes object and other objects of type uilabel, uibutton. The axes object with title Peak Surface contains an object of type surface.

    Use f to change the font of all text in the figure to the default system font.

    fontname(f,"default")

    Figure contains an axes object and other objects of type uilabel, uibutton. The axes object with title Peak Surface contains an object of type surface.

    Input Arguments

    collapse all

    Font name, specified as a character vector or string scalar. Use listfonts for a list of available system fonts.

    Object or container with text, specified as a graphics object or array of graphics objects. The fontname function affects text in the specified objects. If obj contains other graphics objects, such as a figure that contains UI components or an axes object that has a legend, the function also sets the font name for those objects within obj.

    Version History

    Introduced in R2022a

    expand all