Categories
3. Matlab Functions

Matlab gca

MATLAB’s ‘gca’ method can be used to get the handle for our current axis. Also, if we don’t have any handle, then the ‘gca’ method will generate one. To refresh our understanding of handle, please keep in mind that handle is basically a number that will refer to an Object. This Object can be a figure, axes, or lines. If we need to perform any changes to the Object, we will require a reference to the object which can be done using the handle.

Syntax:

ca = gca will return the handle to current axes in the figure.

Let us now understand how to get the current axes in MATLAB using ‘gca’ method.

Examples

Let us discuss examples of Matlab gca.

Example 1:

In this example, we will use gca method to get the current axes of our figure. We will plot an exponential function for our first example. The steps to be followed for this example are:

  1. Initialize the function whose current axes is required
  2. Use the plot method to display the figure
  3. Initialize the gca method
  4. Set the font size for the current axes
  5. Set the current axes’ limit for the current axes

Code:

x = linspace (0, 20);
y = exp (2 * x);

[Initializing the x & y axis. Here we are using an exponential function]

plot (x, y)

[Using the plot method to display the figure]

ca = gca

[Using the ‘gca’ method for referring to current axes]

ca.FontSize = 12;

[Setting the font size for the current axes]

ca.YLim = [0 20];

[Setting the current axes’ limit]

This is how our input and output will look like in MATLAB command window:

Input:

matlab gca 1

Output 1:

Plot of the exponential function

matlab gca 2

Output 2:

Getting the current axes of the above figure

matlab gca 3

As we can see in the OUTPUT, we have obtained the current axes of the exponential function defined by us.

Example 2

In this example, we will use gca method to get the current axes of our figure. We will plot a sine wave for this example. The steps to be followed for this example are:

  1. Initialize the function whose current axes is required
  2. Use the plot method to display the figure
  3. Initialize the gca method
  4. Set the font size for the current axes
  5. Set the current axes’ limit for the current axes

Code:

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

[Initializing the x & y axis. Here we are defining a sine wave]

plot (x, y)

[Using the plot method to display the figure]

ca = gca

[Using the ‘gca’ method for referring to current axes]

ca.FontSize = 12;

[Setting the font size for the current axes]

ca.YLim = [-5 5];

[Setting the current axes’ limit for the current axes]

This is how our input and output will look like in MATLAB command window:

Input:

matlab gca 4

Output 1:

Plot of the sine wave

matlab gca 5

Output 2:

Getting the current axes of above figure

matlab gca 6

As we can see in the OUTPUT, we have obtained the current axes of the sine wave defined by us.

Example 3

In this example, we will use gca method to get the current axes of our figure. We will plot a cos wave for this example. The steps to be followed for this example are:

  1. Initialize the function whose current axes is required
  2. Use the plot method to display the figure
  3. Initialize the gca method
  4. Set the font size for the current axes
  5. Set the current axes’ limit for the current axes

Code:

x = linspace (0, 20);
y = cos (10 * x);

[Initializing the x & y axis. Here we are defining a cos wave]

plot (x, y)

[Using the plot method to display the figure]

ca = gca

[Using the ‘gca’ method for referring to current axes]

ca.FontSize = 12;

[Setting the font size for the current axes]

ca.YLim = [-5 5];

[Setting the current axes’ limit for the current axes]

This is how our input and output will look like in Matlab command window:

Input:

matlab gca 7

Output 1:

Plot of the cos wave

example 3

Output 2:

Getting the current axes of above figure

example 3-1

As we can see in the OUTPUT, we have obtained the current axes of the cos wave defined by us.

Example 4

In this example, we will use gca method to get the current axes of our figure. We will plot a logarithmic function for this example. The steps to be followed for this example are:

  1. Initialize the function whose current axes is required
  2. Use the plot method to display the figure
  3. Initialize the gca method
  4. Set the font size for the current axes
  5. Set the current axes’ limit for the current axes

Code:

x = linspace (0, 20);
y = log (10 * x);

[Initializing the x & y axis. Here we are defining a logarithmic function]

plot (x, y)

[Using the plot method to display the figure]

ca = gca

[Using the ‘gca’ method for referring to current axes]

ca.FontSize = 12;

[Setting the font size for the current axes]

ca.YLim = [0 10];

[Setting the current axes’ limit for the current axes]

This is how our input and output will look like in MATLAB command window:

Input:

example 4

Output 1:

Plot of the logarithm function

example 4-1

Output 2:

Getting the current axes of above figure

example 4-2

As we can see in the OUTPUT, we have obtained the current axes of the logarithm function defined by us.

Leave a Reply

Your email address will not be published. Required fields are marked *