Categories

# Matlab xticks

The ‘xticks function’ is used in Matlab to assign tick values & labels to the x-axis of a graph or plot. By default, the plot function (used to draw any plot) in Matlab creates ticks as per the default scale, but we might need to have ticks based on our requirement. Adding ticks as per our need and labelling them make the plots more intuitive and easier to understand. For this, we can use the xticks function along with the xticklabels function in Matlab to identify the values of our choice on the plots easily.

### Syntax of xticks function:

`xticks (A)`

`xticks (A : B : C)`

## Explanation:

• xticks (A) is used to set the ticks defined by the vector A. Please note that A must have values in the ascending order.
• xticks (A: B: C) is used to set the ticks defined by the range A: C, with a B gap between the ticks.

## Examples of Matlab xticks

Given below are the examples of Matlab xticks:

## Example 1:

In this example, we will use the plot function to plot a sine wave and then will set the ticks for it using the xticks function.

Below are the steps to be followed:

• Write the code to create a sine wave.
• Use the xticks function to set the ticks for the x-axis.
• Use the xticklabels function to set the labels for the ticks defined in the above step.

### Code:

``````<!-- wp:paragraph -->
<p>A = 0 : pi/50 : 2*pi;<br>[Initializing the range for sine wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>B = sin (A);<br>[Initializing the sine wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>plot (A, B)<br>[Using the plot function to plot the sine wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>xticks ([0 3 6])<br>[Using the xticks function to set the ticks for the x-axis]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>xticklabels ({‘A = 0′,’A = 3′,’A = 6’})<br>[Using the xticklabels function to set the labels for the ticks]</p>
<!-- /wp:paragraph -->``````

### Output:

As we can see in the output, we have obtained ticks of our choice, i.e. 0, 3, 6, using the xticks function. We have also set the labels for these ticks using the xticklabels function.

## Example 2:

In this example, we will use the plot function to plot a cos wave and then will set the ticks for it using the xticks function.

Below are the steps to be followed:

• Write the code to create a cos wave.
• Use the xticks function to set the ticks for the x-axis.
• Use the xticklabels function to set the labels for the ticks defined in the above step.

### Code:

``````<!-- wp:paragraph -->
<p>A = 0 : pi/100 : 3*pi;<br>[Initializing the range for cos wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>B = cos (A);<br>[Initializing the cos wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>plot (A, B)<br>[Using the plot function to plot the cos wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>xticks ([0 3 6 9])<br>[Using the xticks function to set the ticks for the x-axis]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>xticklabels ({‘A = 0′,’A = 3′,’A = 6’, ‘A = 9’})<br>[Using the xticklabels function to set the labels for the ticks]</p>
<!-- /wp:paragraph -->``````

### Output:

As we can see in the output, we have obtained ticks of our choice, i.e. 0, 3, 6, 9, using the xticks function. We have also set the labels for these ticks using the xticklabels function.

In the above 2 examples, we have passed all the values which we want to set as the ticks of a plot as arguments to the xticks function.

Next, we will see how to set a range of values with a fixed interval as the ticks of a plot.

## Example 3:

In this example, we will use the plot function to plot a sine wave and then will set the ticks for it by passing a range as an argument to the xticks function.

Below are the steps to be followed:

• Write the code to create a sine wave.
• Use the xticks function to set the ticks for the x-axis.
• Use the xticklabels function to set the labels for the ticks defined in the above step.

### Code:

``````<!-- wp:paragraph -->
<p>A = linspace (0, 50)<br>[Initializing the range for sine wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>B = sin (A);<br>[Initializing the sine wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>plot (A, B)<br>[Using the plot function to plot the sine wave]</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>xticks ([0 : 10 : 50])<br>[Using the xticks function to set the ticks for the x-axis]</p>
<!-- /wp:paragraph -->``````

### Output:

As we can see in the output, we have obtained ticks of our choice by passing a range as an argument to the xticks function.