MATLAB Answers

How to change the x axis labels of a plot so that they only show integer values

73 views (last 30 days)
AStar
AStar on 15 Nov 2019
Edited: Walter Roberson on 12 Mar 2020
Id like to make the x axis labels so that it just shows 1,2,3,4,5,6. But id like to make sure this is general so in my code if the user were to input 10 years lets say, the x axis labels would be 1,2,3,4,5,6,7,8,9,10.
Thanks!

  2 Comments

Walter Roberson
Walter Roberson on 15 Nov 2019
What datatype is the x axes? duration()? Or is it double and you are using datetick() ?
AStar
AStar on 15 Nov 2019
the x values are a vector. Im not sure what you mean by duration, double and datetick. I am a beginner to a matlab, so I dont know what terminology youre referring to.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 15 Nov 2019
ax = gca;
ax.Xticks = unique( round(ax.XTicks) );

  2 Comments

AStar
AStar on 15 Nov 2019
Hi, thanks for your response, however, it doesnt seem to work. What is ax? Is that the name of the x vector?Matlab also says that it doesnt recognize XTicks.
Walter Roberson
Walter Roberson on 12 Mar 2020
ax is a variable assigned the result of gca . gca is a function that returns a handle to the current axes. So ax will be assigned a handle to the current axes.
ax = gca;
ax.XTick = unique( round(ax.XTick) );

Sign in to comment.


Soohyun Cho
Soohyun Cho on 12 Mar 2020
curtick = get(gca, 'xTick');
xticks(unique(round(curtick)));

  1 Comment

Walter Roberson
Walter Roberson on 12 Mar 2020
Note: this requires R2016b or later. For earlier releases, especially before R2015b, it would look like,
curtick = get(gca, 'XTick');
set(gca, 'XTick', unique(round(curtick)))

Sign in to comment.

Tags


Translated by