Main Content

ysecondarylabel

Set y-axis secondary label

Since R2023b

    Description

    example

    ysecondarylabel(label) displays the specified text label as a secondary label on the y-axis of the current axes. If the y-axis already has a secondary label, the specified label replaces it.

    example

    ysecondarylabel(Visible="off") hides the secondary label. You can specify the value of Visible as false or 0 to produce the same result. For example, ysecondarylabel(Visible=0) also hides the label.

    ysecondarylabel(Visible="on") displays the secondary label. You can specify the value of Visible as true or 1 to produce the same result. For example, ysecondarylabel(Visible=true) also displays the label. This syntax has no effect when the label is empty.

    ysecondarylabel("auto") resets the secondary label to its automatic state, which displays a secondary label in some cases. For example, a secondary label might automatically appear when you plot datetime values or very large numbers.

    example

    ysecondarylabel(ax,___) modifies the secondary label for the target axes. Specify the target axes before any of the input argument combinations in the previous syntaxes.

    Examples

    collapse all

    Plot a vector of wattage values against a vector of voltage values. Then create an x-axis label, a y-axis label, and a secondary label.

    volts = [10 20 30 40 50];
    amps = 0.9;
    watts = volts * amps;
    
    plot(volts,watts)
    xlabel("Volts")
    ylabel("Watts")
    ysecondarylabel("(1 Watt = 1 J/s)")

    Figure contains an axes object. The axes object with xlabel Volts, ylabel Watts contains an object of type line.

    Plot a vector of duration values against a vector of numbers.

    x = 1:5;
    y = minutes([3 1 6 4 10]);
    plot(x,y)

    Figure contains an axes object. The axes object contains an object of type line.

    Hide the secondary label on the y-axis. The "min" label disappears.

    ysecondarylabel(Visible="off")

    Figure contains an axes object. The axes object contains an object of type line.

    Create two plots in a tiled chart layout. When you call the nexttile function, specify an output argument to get the axes object for later use.

    tiledlayout(1,2,Padding="tight")
    volts = [10 20 30 40 50];
    
    % First plot
    amps1 = 0.9;
    watts1 = volts * amps1;
    ax1 = nexttile;
    plot(volts, watts1)
    xlabel("Volts")
    ylabel("Watts")
    
    % Second plot
    amps2 = 1.1;
    watts2 = volts * amps2;
    ax2 = nexttile;
    plot(volts,watts2)
    xlabel("Volts")
    ylabel("Watts")

    Figure contains 2 axes objects. Axes object 1 with xlabel Volts, ylabel Watts contains an object of type line. Axes object 2 with xlabel Volts, ylabel Watts contains an object of type line.

    Add a secondary label, "(1 Watt = 1 J/s)", to the y-axes of both plots by specifying the array [ax1 ax2] as the first argument to the ysecondarylabel function.

    ysecondarylabel([ax1 ax2],"(1 Watt = 1 J/s)")

    Figure contains 2 axes objects. Axes object 1 with xlabel Volts, ylabel Watts contains an object of type line. Axes object 2 with xlabel Volts, ylabel Watts contains an object of type line.

    Input Arguments

    collapse all

    Label text, specified as a string scalar, string vector, character vector, or cell array of character vectors. To display one line of text, specify a string scalar or character vector. To specify multiple lines of text, specify a string vector or cell array of character vectors.

    Example: ysecondarylabel("Hurray for me!")

    Example: ysecondarylabel('Hurray for you!')

    Target axes, specified as an Axes object or an array of Axes objects.

    If you do not specify the target axes, then ysecondarylabel uses the current axes.

    Version History

    Introduced in R2023b