EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab xticks
 

Matlab xticks

Updated July 10, 2023

Matlab xticks

 

 

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, Matlab’s plot function (used to draw any plot) creates ticks as per the default scale, but we might need to have ticks based on our requirements. Adding ticks 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 easily identify the values of our choice on the plots.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of xticks function:

xticks (A)
xticks (A : B : C)

Explanation:

  • You use xticks(A) to set the ticks defined by the vector A. Please note that A must have values in ascending order.
  • You use xticks(A: B: C) to set the ticks defined by the range A to C with a gap of B 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 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:

Matlab xticks 1

Output:

plot a sine wave

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

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:

Matlab xticks 3

Output:

plot a cos wave

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 we want to set as plot ticks 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:

Matlab xticks 5

Output:

to plot a sine wave

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. A range and a set of values can be passed as an argument to the xticks function.

Recommended Articles

We hope that this EDUCBA information on “Matlab xticks” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. What is Matlab?
  2. Matlab limit
  3. Plot graph Matlab
  4. Absolute Value Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW