gca

Current axes handle

Syntax

Description

example

ax = gca returns the handle to the current axes for the current figure. If an axes does not exist, then gca creates an axes and returns its handle. You can use the axes handle to query and modify axes properties. For more information, see Axes Properties.

Examples

expand all

Specify Properties for Current Axes

Plot a sine wave.

x = linspace(0,10);
y = sin(4*x);
plot(x,y)

Use the gca function to get the handle of the current axes. Then, set the font size, tick direction, tick length, and y-axis limits for the current axes. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

ax = gca; % handle of current axes
ax.FontSize = 12;
ax.TickDir = 'out';
ax.TickLength = [0.02,0.02];
ax.YLim = [-2,2];

More About

expand all

Current Axes

The current axes is the target for graphics output. It is the axes in which graphics commands such as plot, text, and surf draw their results. It is typically the last axes created or the last axes clicked with the mouse. Changing the current figure also changes the current axes.

User interaction can change the current axes. If you need to access a specific axes, store the axes handle in your program code when you create the axes and use this handle instead of gca.

Tips

  • To get the handle of the current axes without forcing the creation of an axes if one does not exist, query the figure CurrentAxes property.

    fig = gcf;
    ax = fig.CurrentAxes;

    MATLAB® returns ax as an empty array if there is no current axes.

  • Set axes properties after plotting since some plotting functions reset axes properties.

See Also

Functions

Properties

Was this topic helpful?