## Introduction to 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:**

A = 0 : pi/50 : 2*pi;

[Initializing the range for sine wave]

B = sin (A);

[Initializing the sine wave]

plot (A, B)

[Using the plot function to plot the sine wave]

xticks ([0 3 6])

[Using the xticks function to set the ticks for the x-axis]

xticklabels ({‘A = 0′,’A = 3′,’A = 6’})

[Using the xticklabels function to set the labels for the ticks]

**Input:**

**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:**

4.5 (7,668 ratings)

View Course

A = 0 : pi/100 : 3*pi;

[Initializing the range for cos wave]

B = cos (A);

[Initializing the cos wave]

plot (A, B)

[Using the plot function to plot the cos wave]

xticks ([0 3 6 9])

[Using the xticks function to set the ticks for the x-axis]

xticklabels ({‘A = 0′,’A = 3′,’A = 6’, ‘A = 9’})

[Using the xticklabels function to set the labels for the ticks]

**Input:**

**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:**

A = linspace (0, 50)

[Initializing the range for sine wave]

B = sin (A);

[Initializing the sine wave]

plot (A, B)

[Using the plot function to plot the sine wave]

xticks ([0 : 10 : 50])

[Using the xticks function to set the ticks for the x-axis]

**Input:**

**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.

### Conclusion

The xticks function is used in Matlab to assign tick values to the x-axis. The xticklabels function can be used along with the xticks function to label the ticks assigned. Both a range and a set of values can be passed as an argument to the xticks function.

### Recommended Articles

This is a guide to Matlab xticks. Here we discuss the introduction and the examples of the Matlab xticks for better understanding. You may also have a look at the following articles to learn more –