Main Content

fontname

Change font name for objects in a figure

    Description

    example

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

    example

    fontname(obj,"default") resets the font name back to the default, automatic font name value for obj.

    Examples

    collapse all

    Create a plot of cosine curves, and add a plot title and legend.

    x = linspace(0,pi);
    y1 = cos(x);
    y2 = cos(2*x);
    y3 = cos(3*x);
    plot(x,y1,x,y2,x,y3)
    legend("cos(x)","cos(2x)","cos(3x)")
    title("2-D Line Plot")

    Figure contains an axes object. The axes object with title 2-D Line Plot contains 3 objects of type line. These objects represent cos(x), cos(2x), cos(3x).

    Set the font of all text within the figure to Courier. Use the object returned by gcf as the target object to apply this change.

    fontname(gcf,"Courier")

    Figure contains an axes object. The axes object with title 2-D Line Plot contains 3 objects of type line. These objects represent cos(x), cos(2x), cos(3x).

    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;

    {"String":"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.","Tex":"Peak Surface","LaTex":[]}

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

    fontname(f,"default")

    {"String":"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.","Tex":"Peak Surface","LaTex":[]}

    Input Arguments

    collapse all

    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.

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

    Version History

    Introduced in R2022a